Skip to content

Commit f7fc946

Browse files
same tree
1 parent ef23e2b commit f7fc946

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

EASY/src/easy/SameTree.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package easy;
2+
3+
import classes.TreeNode;
4+
5+
/**100. Same Tree
6+
7+
Total Accepted: 145623
8+
Total Submissions: 330537
9+
Difficulty: Easy
10+
11+
Given two binary trees, write a function to check if they are equal or not.
12+
13+
Two binary trees are considered equal if they are structurally identical and the nodes have the same value. */
14+
public class SameTree {
15+
//recursion idea flows out naturally.
16+
public boolean isSameTree(TreeNode p, TreeNode q) {
17+
if(p == null || q == null) return p == q;
18+
if(p.val != q.val) return false;
19+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
20+
}
21+
}

0 commit comments

Comments
 (0)