forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJeehay28.ts
More file actions
62 lines (43 loc) · 1.23 KB
/
Jeehay28.ts
File metadata and controls
62 lines (43 loc) · 1.23 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
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}
// TC: O(n)
// SC: O(n)
function invertTree(root: TreeNode | null): TreeNode | null {
if (!root) return null;
const left = invertTree(root.right);
const right = invertTree(root.left);
root.left = left;
root.right = right;
return root;
}
// TC: O(n)
// SC: O(n)
// function invertTree(root: TreeNode | null): TreeNode | null {
// if (!root) return null;
// [root.left, root.right] = [root.right, root.left];
// invertTree(root.left);
// invertTree(root.right);
// return root;
// }
// TC: O(n)
// SC: O(n)
// function invertTree(root: TreeNode | null): TreeNode | null {
// if (!root) return null;
// const stack: (TreeNode | null)[] = [root];
// while (stack.length > 0) {
// const node = stack.pop();
// if (!node) continue;
// [node.left, node.right] = [node.right, node.left];
// stack.push(node.left);
// stack.push(node.right);
// }
// return root;
// }