-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path101 Symmetric Tree.js
More file actions
88 lines (74 loc) · 1.9 KB
/
Copy path101 Symmetric Tree.js
File metadata and controls
88 lines (74 loc) · 1.9 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
// For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
// 1
// / \
// 2 2
// / \ / \
// 3 4 4 3
// But the following [1,2,2,null,3,null,3] is not:
// 1
// / \
// 2 2
// \ \
// 3 3
// Note:
// Bonus points if you could solve it both recursively and iteratively.
// Hide Company Tags LinkedIn Bloomberg Microsoft
// Hide Tags Tree Depth-first Search Breadth-first Search
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
// Iterative solution 68 - 72ms and 36MB. Not the best.
var isSymmetric = function(root) {
let queue = [];
queue.push(root);
while (queue.length != 0) {
let len = queue.length;
if (!checkSymmetry(queue)) return false;
for (let i = 0; i < len; i++) {
let node = queue.shift();
if (node !== null) {
queue.push(node.left);
queue.push(node.right);
}
}
}
return true;
};
const checkSymmetry = nodes => {
let left = 0;
let right = nodes.length - 1;
while (left < right) {
if (
(nodes[left] === null && nodes[right] === null) ||
(nodes[left] && nodes[right] && nodes[left].val === nodes[right].val)
) {
left++;
right--;
} else {
return false;
}
}
return true;
};
// Recursive solution 64ms faster than 100% and 35.6MB less than ~30%.
var isSymmetric = function(root) {
return recursiveSymmetry(root, root);
};
const recursiveSymmetry = (root1, root2) => {
if (root1 === null && root2 === null) return true;
if (root1 === null || root2 === null) return false;
return (
root1.val === root2.val &&
recursiveSymmetry(root1.left, root2.right) &&
recursiveSymmetry(root1.right, root2.left)
);
};