Skip to content

Commit d8759ae

Browse files
committed
feat: solve new question
1 parent 9002b76 commit d8759ae

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

invert-binary-tree/main.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/*
10+
[4,2,7,1,3,6,9]
11+
12+
4
13+
/ \
14+
2 7
15+
/ \ / \
16+
1 3 6 9
17+
18+
*/
19+
/**
20+
* @param {TreeNode} root
21+
* @return {TreeNode}
22+
*/
23+
var invertTree = function(root) {
24+
if (root === null) return null;
25+
26+
// swap the children
27+
let tmp = root.left;
28+
root.left = root.right;
29+
root.right = tmp;
30+
31+
// do it again until root of the tree
32+
invertTree(root.left);
33+
invertTree(root.right);
34+
35+
return root;
36+
};

0 commit comments

Comments
 (0)