-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleetcode-226-InvertBinaryTree.js
98 lines (78 loc) · 2.07 KB
/
leetcode-226-InvertBinaryTree.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
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
89
90
91
92
93
94
95
96
97
98
// https://leetcode.com/problems/invert-binary-tree/
function TreeNode(val, left, right) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
// p: root array
// r: array
// e:
// Input: root = [4,2,7,1,3,6,9]
// Output: new root [4,7,2,9,6,3,1]
// Input: root = [2,1,3]
// Output: [2,3,1]
// Input: root = []
// Output: []
// Recursion
var invertTree = function (root) {
if (!root) return null;
let curL = root.left;
let curR = root.right;
root.right = curL;
root.left = curR;
if (root.left) invertTree(root.left);
if (root.right) invertTree(root.right);
return root;
};
const a = new TreeNode();
const b = new TreeNode();
// const c = new TreeNode();
// const d = new TreeNode();
// const e = new TreeNode();
// const f = new TreeNode();
// const g = new TreeNode();
a.val = 2;
a.left = b;
b.val = 1;
// b.left = c;
// b.val = 1
// b.right = d;
// a.right = e;
// e.val = 3;
// e.left = f;
// e.right = g;
invertTree(a);
// // DFS
// var invertTree = function (root) {
// console.log(root);
// if (!root) return null;
// // console.log(root);
// const stack = [root];
// while (stack.length > 0) {
// const current = stack.pop();
// const tempR = current.right;
// const tempL = current.left;
// current.left = tempR;
// current.right = tempL;
// current.right && stack.push(current.right);
// current.left && stack.push(current.left);
// }
// console.log(root);
// return root;
// };
// // BFS
// var invertTree = function (root) {
// if (!root) return null;
// // console.log(root);
// const queue = [root];
// while (queue.length > 0) {
// const current = queue.shift();
// const curL = current.left;
// const curR = current.right;
// current.left = curR;
// current.right = curL;
// current.right && queue.push(current.right);
// current.left && queue.push(current.left);
// }
// return root;
// };