-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path0100_SameTree.js
52 lines (52 loc) · 1.1 KB
/
0100_SameTree.js
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
44
45
46
47
48
49
50
51
52
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function(p, q) {
//Recursive solution
// function helper(p, q){
// if(!p && !q){
// return true
// }
// if(!p || !q){
// return false
// }
// if(p.val != q.val){
// return false
// }
// return helper(p.left,q.left) && helper(p.right,q.right)
// }
// return helper(p,q)
var queue = [[p,q]];
function check(p,q){
if(!p && !q){
return true;
}
if(!p || !q){
return false
}
if(p.val != q.val){
return false
}
return true;
}
while(queue.length > 0){
let [p, q] = queue.pop();
if(!check(p,q)){
return false;
}
if(p){
queue.push([p.left, q.left]);
queue.push([p.right, q.right]);
}
}
return true;
};