-
Notifications
You must be signed in to change notification settings - Fork 1
/
101-Symmetric Tree.js
60 lines (50 loc) · 1.29 KB
/
101-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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Problem link: https://leetcode.com/problems/symmetric-tree
* 101: Symmetric Tree
*
* Input: root = [1,2,2,3,4,4,3]
* Output: true
*
* Input: root = [1,2,2,null,3,null,3]
* Output: false
*
* Solution: Binary Tree, DFS, BFS
*/
class Node {
constructor(value) {
this.left = null;
this.right = null;
this.val = value;
}
}
/**
* this is better
* @param root
*/
var isSymmetric = function(root) {
if (!root) {
return true;
}
var checkSymmetric = function (left, right) {
if (left == null && right == null) {
return true;
}
if ((left == null || right == null) || (left.val != right.val)) {
return false;
}
if (left && right) {
return checkSymmetric(left.left, right.right) && checkSymmetric(left.right, right.left) ;
}
}
return checkSymmetric(root.left, root.right);
};
root = new Node(1);
root.left = new Node(2);
root.right = new Node(2);
root.right.left = new Node(4);
root.right.right = new Node(3);
root.left.left = new Node(3);
root.left.right = new Node(4);
// console.log("result 1: ", root);
// console.log("result 1: ", maxDepthBfs (root));
console.log("result 1: ", isSymmetric (root));