Skip to content

Commit ee3bc23

Browse files
committed
solved question 226
1 parent 60200c5 commit ee3bc23

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

.DS_Store

6 KB
Binary file not shown.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Invert Binary Tree
2+
3+
// Problem 226
4+
// Given the root of a binary tree, invert the tree, and return its root.
5+
6+
// Example 1:
7+
// Input: root = [4,2,7,1,3,6,9]
8+
// Output: [4,7,2,9,6,3,1]
9+
10+
// Example 2:
11+
// Input: root = [2,1,3]
12+
// Output: [2,3,1]
13+
14+
// Example 3:
15+
// Input: root = []
16+
// Output: []
17+
18+
/**
19+
* @param {TreeNode} root
20+
* @return {TreeNode}
21+
*/
22+
const invertTree = function(root) {
23+
if (root) {
24+
[root.left, root.right] = [root.right, root.left];
25+
if (root.left) {
26+
invertTree(root.left);
27+
}
28+
if (root.right) {
29+
invertTree(root.right);
30+
}
31+
}
32+
return root;
33+
};

javascript/questions-200-300/question-226/question-226.test.js

Whitespace-only changes.

javascript/util/TreeNode.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Definition for a binary tree node.
3+
*/
4+
function TreeNode(val, left, right) {
5+
this.val = val === undefined ? 0 : val;
6+
this.left = left === undefined ? null : left;
7+
this.right = right === undefined ? null : right;
8+
}

0 commit comments

Comments
 (0)