Skip to content

Commit f9d9929

Browse files
committed
solve: 226. Invert Binary Tree
1 parent c45004c commit f9d9929

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

invert-binary-tree/evan.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function TreeNode(val, left, right) {
2+
this.val = val === undefined ? 0 : val;
3+
this.left = left === undefined ? null : left;
4+
this.right = right === undefined ? null : right;
5+
}
6+
/**
7+
* @param {TreeNode} root
8+
* @return {TreeNode}
9+
*/
10+
var invertTree = function (root) {
11+
if (!root) {
12+
return null;
13+
}
14+
15+
const originLeft = root.left;
16+
const originRight = root.right;
17+
18+
root.right = invertTree(originLeft);
19+
root.left = invertTree(originRight);
20+
21+
return root;
22+
};

0 commit comments

Comments
 (0)