Skip to content

Commit a6fec4a

Browse files
committedJun 25, 2023
✨ [226]
1 parent 8228b78 commit a6fec4a

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed
 

‎226/my_solution.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class TreeNode {
2+
constructor(val, left, right) {
3+
this.val = (val === undefined ? 0 : val)
4+
this.left = (left === undefined ? null : left)
5+
this.right = (right === undefined ? null : right)
6+
}
7+
}
8+
/**
9+
* @param {TreeNode} root
10+
* @return {TreeNode}
11+
*/
12+
function invertTree(root) {
13+
if (null === root) return null;
14+
if (null === root.val) return new TreeNode();
15+
16+
const invertNode = (node) => {
17+
if (null === node) return node;
18+
let left = node.left, right = node.right;
19+
20+
if (null !== node.left) {
21+
node.right = left;
22+
// node.left = right;
23+
invertNode(node.right);
24+
}
25+
26+
if (null !== node.right) {
27+
node.left = right;
28+
node.right = left;
29+
invertNode(node.left);
30+
}
31+
32+
33+
return node;
34+
}
35+
36+
// while (queue.length > 0) {
37+
// let curr = queue.shift(), left = curr.left, right = curr.right;
38+
//
39+
// if (null !== left) {
40+
// queue.push(left);
41+
// dummy.right = left;
42+
// }
43+
//
44+
// if (null !== right) {
45+
// queue.push(right);
46+
// dummy.left = right;
47+
// }
48+
//
49+
// dummy = dummy;
50+
// }
51+
52+
return invertNode(root);
53+
}
54+
55+
//[4,2,7,1,3,6,9]
56+
let x =
57+
// invertTree(new TreeNode(4,
58+
// new TreeNode(2, new TreeNode(1), new TreeNode(3)),
59+
// new TreeNode(7, new TreeNode(6), new TreeNode(9))
60+
// ));
61+
// invertTree(new TreeNode(1,
62+
// new TreeNode(2)
63+
// ));
64+
invertTree(new TreeNode(1,
65+
null,
66+
new TreeNode(2)
67+
));
68+
69+
console.log("Result")
70+
console.log(x)

‎226/solution.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class TreeNode {
2+
constructor(val, left, right) {
3+
this.val = (val === undefined ? 0 : val)
4+
this.left = (left === undefined ? null : left)
5+
this.right = (right === undefined ? null : right)
6+
}
7+
}
8+
/**
9+
* @param {TreeNode} root
10+
* @return {TreeNode}
11+
*/
12+
function invertTree(root) {
13+
if (null === root) return null;
14+
if (null === root.val) return new TreeNode();
15+
16+
const invertNode = (node) => {
17+
if (null === node) return node;
18+
let left = node.left, right = node.right;
19+
20+
if (null !== node.left) {
21+
node.right = left;
22+
// node.left = right;
23+
invertNode(node.right);
24+
}
25+
26+
if (null !== node.right) {
27+
node.left = right;
28+
node.right = left;
29+
invertNode(node.left);
30+
}
31+
32+
return node;
33+
}
34+
35+
return invertNode(root);
36+
}

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
- [207. Course Schedule](./207/)
5656
- [213. House Robber II](./213/)
5757
- [217. Contains Duplicate](./217/)
58+
- [226. Invert Binary Tree](./226/)
5859
- [238. Product of Array Except Self](./238/)
5960
- [242. Valid Anagram](./242/)
6061
- [268. Missing Number](./268/)
@@ -139,7 +140,7 @@ Batch create:
139140
NOTE: JS IS HERE
140141
-->
141142
```ssh
142-
chapter=100 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
143+
chapter=226 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
143144
```
144145
> then you can use `x` for quick debug.
145146

0 commit comments

Comments
 (0)
Please sign in to comment.