-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathleetcode-101-SymmetricTree-2.js
More file actions
43 lines (36 loc) · 973 Bytes
/
leetcode-101-SymmetricTree-2.js
File metadata and controls
43 lines (36 loc) · 973 Bytes
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
/**
* 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}
*/
// p: root of bt;
// r: boolean;
// e:
// iter
var isSymmetric = function (root) {
const stack = [[root.left, root.right]];
while (stack.length > 0) {
const [r1, r2] = stack.pop();
if (!r1 && !r2) continue;
if (!r1 || !r2 || r1.val !== r2.val) return false;
stack.push([r1.left, r2.right]);
stack.push([r1.right, r2.left]);
}
return true;
};
// recu
// var isSymmetric = function(root) {
// return (check(root.left, root.right));
// };
// const check = (r1, r2) => {
// if (!r1 && !r2) return true;
// if (!r1 || !r2 || r1.val !== r2.val) return false;
// return check(r1.right, r2.left) && check(r2.right, r1.left);
// }