-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymmetric-tree.ts
39 lines (36 loc) · 1.05 KB
/
symmetric-tree.ts
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
import { TreeNode } from "lc/tools/tree-node";
function isMirror(l: TreeNode | null, r: TreeNode | null): boolean {
if (l === null && r === null) {
return true;
}
if (l === null || r === null) {
return false;
}
return l.val === r.val && isMirror(l.right, r.left) && isMirror(l.left, r.right);
}
export function isSymmetric(root: TreeNode | null): boolean {
return isMirror(root, root);
}
export function isSymmetricIterative(root: TreeNode | null): boolean {
if (root === null) {
return true;
}
let queue: Array<TreeNode | null> = [];
queue.push(root, root);
while (queue.length !== 0) {
let l = queue.shift() as TreeNode | null;
let r = queue.shift() as TreeNode | null;
if (l === null && r === null) {
continue;
}
if (l === null || r === null) {
return false;
}
if (l.val !== r.val) {
return false;
}
queue.push(l.right, r.left);
queue.push(l.left, r.right);
}
return true;
}