-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsame-tree.js
52 lines (43 loc) · 1.32 KB
/
same-tree.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, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function(p, q) {
let n1 = p == null
let n2 = q == null
if(n1 && n2) return true
if(n1 ^ n2) return false
if(p.val != q.val) return false
let queue1 = [p]
let queue2 = [q]
while(queue1.length > 0 && queue1.length == queue2.length){
var c1 = queue1.shift();
var c2 = queue2.shift();
let cl1 = c1.left != null
let cl2 = c2.left != null
let cr1 = c1.right != null
let cr2 = c2.right != null
if(cl1 && cl2 && c1.left.val != c2.left.val) return false
else if(cl1 ^ cl2) return false
else if(cl1 && cl2){
queue1.push(c1.left)
queue2.push(c2.left)
}
if(cr1 && cr2 && c1.right.val != c2.right.val) return false
else if(cr1 ^ cr2) return false
else if(cr1 && cr2){
queue1.push(c1.right)
queue2.push(c2.right)
}
}
return queue1.length + queue2.length == 0
};