-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymmetric-tree.js
46 lines (43 loc) · 1.21 KB
/
symmetric-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
var isSymmetric = function(root) {
// let queue = [];
// if(root===null) return true
// queue.push(root.left);
// queue.push(root.right);
// while(queue.length){
// let left = queue.shift();
// let right = queue.shift();
// if(left===null&&right===null){
// continue
// };
// if(left.val!=right.val){
// return false
// }
// if(left===null||right===null){
// return false
// };
// queue.push(left.left);
// queue.push(right.right);
// queue.push(left.right);
// queue.push(right.left);
// }
// return true
if (root == null) return true;
const queue = [];
queue.push(root.left);
queue.push(root.right);
while (queue.length) {
let left = queue.shift();
let right = queue.shift();
if(left == null && right == null){
continue;
}else if(left == null || right == null || left.val != right.val){
return false;
}
queue.push(left.left);
queue.push(right.right);
queue.push(left.right);
queue.push(right.left);
}
return true;
};
isSymmetric([1,2,2,3,4,4,3]);