Skip to content

Commit 619d4b8

Browse files
authored
Create 101-symmetric-tree.js
1 parent 69f16be commit 619d4b8

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

101-symmetric-tree.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {boolean}
11+
*/
12+
const isSymmetric = function(root) {
13+
if(root == null) return true
14+
return compare(root.left, root.right)
15+
};
16+
17+
function compare(l, r) {
18+
if(l == null && r == null) return true
19+
if( (l == null && r != null) || (l != null && r == null) ) return false
20+
21+
if(l.val === r.val) {
22+
if(compare(l.left, r.right) !== false && compare(l.right, r.left) !== false) {
23+
return true
24+
} else {
25+
return false
26+
}
27+
28+
} else {
29+
return false
30+
}
31+
}

0 commit comments

Comments
 (0)