Skip to content

Commit 9c3e0e5

Browse files
committed
Added invert binary tree solution
1 parent 8df62a4 commit 9c3e0e5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

invert-binary-tree/nhistory.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
* @param {TreeNode} root
11+
* @return {TreeNode}
12+
*/
13+
var invertTree = function (root) {
14+
// Check root is null
15+
if (!root) return null;
16+
// Create left and right variable to make recurrsive
17+
let left = root.left;
18+
let right = root.right;
19+
// Execute invertTree functino
20+
root.left = invertTree(right);
21+
root.right = invertTree(left);
22+
return root;
23+
};
24+
25+
// TC: O(n)
26+
// SC: O(n)

0 commit comments

Comments
 (0)