forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsora0319.java
More file actions
26 lines (23 loc) · 734 Bytes
/
sora0319.java
File metadata and controls
26 lines (23 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
Stack<TreeNode[]> stack = new Stack<>();
stack.push(new TreeNode[]{p, q});
while (!stack.isEmpty()) {
TreeNode[] nodes = stack.pop();
TreeNode n1 = nodes[0];
TreeNode n2 = nodes[1];
if (n1 == null && n2 == null) {
continue;
}
if (n1 == null || n2 == null) {
return false;
}
if (n1.val != n2.val) {
return false;
}
stack.push(new TreeNode[]{n1.left, n2.left});
stack.push(new TreeNode[]{n1.right, n2.right});
}
return true;
}
}