-
Notifications
You must be signed in to change notification settings - Fork 0
/
101_SymmetricTree.js
42 lines (35 loc) · 983 Bytes
/
101_SymmetricTree.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
// https://leetcode.com/problems/symmetric-tree/#/description
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function (root) {
if (!root) return true;
var isSame = function (lNode, rNode) {
if (lNode === null && rNode === null) return true;
if (lNode !== null && rNode !== null) {
return (lNode.val === rNode.val) && isSame(lNode.left, rNode.right) && isSame(lNode.right, rNode.left);
}
return false;
}
return isSame(root.left, root.right);
};
var tree = new TreeNode(1);
tree.left = new TreeNode(2);
tree.right = new TreeNode(2);
tree.left.left = new TreeNode(3);
tree.left.right = new TreeNode(4);
tree.right.left = new TreeNode(4);
tree.right.right = new TreeNode(3);
console.log(isSymmetric(tree));