-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL100.java
More file actions
21 lines (21 loc) · 719 Bytes
/
L100.java
File metadata and controls
21 lines (21 loc) · 719 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution100 {
class Solution {
/**
* 100. Same Tree https://leetcode.com/problems/same-tree/description/
* @timeComplexity O(n)
* @spaceComplexity O(1) ignoring recursion stack space
* @param p Root of the tree 1
* @param q Root of the tree 2
* @return Whether trees are structurally same
*/
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
}