Skip to content

Commit 9e39a15

Browse files
committed
Symmetric Tree
1 parent 618b11d commit 9e39a15

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Symmetric Tree.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
2+
3+
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
4+
5+
1
6+
/ \
7+
2 2
8+
/ \ / \
9+
3 4 4 3
10+
But the following [1,2,2,null,3,null,3] is not:
11+
1
12+
/ \
13+
2 2
14+
\ \
15+
3 3*/
16+
17+
/**
18+
* Definition for a binary tree node.
19+
* function TreeNode(val) {
20+
* this.val = val;
21+
* this.left = this.right = null;
22+
* }
23+
*/
24+
/**
25+
* @param {TreeNode} root
26+
* @return {boolean}
27+
*/
28+
var isSymmetric = function(root) {
29+
return root == null || isSymmetricHelper(root.left,root.right)
30+
};
31+
32+
var isSymmetricHelper = function(left,right){
33+
if(left == null || right == null)
34+
return left == right
35+
if(left.val != right.val)
36+
return false
37+
return isSymmetricHelper(left.left,right.right) && isSymmetricHelper(left.right,right.left)
38+
};

0 commit comments

Comments
 (0)