Skip to content

Commit 5f1ed68

Browse files
committed
Same Tree
1 parent edca7a0 commit 5f1ed68

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

same-tree/TonyKim9401.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC: O(n)
2+
// retreive all given nodes
3+
// SC: O(1)
4+
// doesn't require additional space
5+
class Solution {
6+
public boolean isSameTree(TreeNode p, TreeNode q) {
7+
if (p == null || q == null) {
8+
if (p == null && q == null) return true;
9+
return false;
10+
}
11+
12+
if (p.val != q.val) return false;
13+
14+
return isSameTree(p.left, q.left) &&
15+
isSameTree(p.right, q.right);
16+
}
17+
}

0 commit comments

Comments
 (0)