forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0101-symmetric-tree.js
57 lines (49 loc) · 1.28 KB
/
0101-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
/**
* 101. Symmetric Tree
* https://leetcode.com/problems/symmetric-tree/
* Difficulty: Easy
*
* Given the root of a binary tree, check whether it is a mirror
* of itself (i.e., symmetric around its center).
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
return isBalanced(root.left, root.right);
};
function isBalanced(a, b) {
if (!a && !b) {
return true;
}
if (!a || !b) {
return false;
}
return a.val === b.val && isBalanced(a.left, b.right) && isBalanced(a.right, b.left);
}
// Top solver not working
// function isSymmetric(root) {
// if (!root) return true;
// let stack = [root.left, root.right];
// while (stack.length) {
// let right = stack.pop();
// let left = stack.pop();
// if (!left && !right) continue;
// if (!left || !right) return false;
// if (left.val !== right.val) return false;
// stack.push(left.left);
// stack.push(right.right);
// stack.push(left.right);
// stack.push(right.left);
// }
// return true;
// }