-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100_SameTree.java
More file actions
43 lines (42 loc) · 1.31 KB
/
100_SameTree.java
File metadata and controls
43 lines (42 loc) · 1.31 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Given two binary trees, write a function to check if they are equal or not.
* Two binary trees are considered equal if they are structurally identical
* and the nodes have the same value.
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//recursive
public class Solution {
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(q.right, p.right);
}
}
//iterative
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.push(p);
stack2.push(q);
while(!stack1.isEmpty() && !stack2.isEmpty()) {
TreeNode n1 = stack1.pop();
TreeNode n2 = stack2.pop();
if(n1 == null && n2 == null) continue;
if(n1 == null || n2 == null || n1.val != n2.val) return false;
stack1.push(n1.right);
stack2.push(n2.right);
stack1.push(n1.left);
stack2.push(n2.left);
}
return true;
}
}