Skip to content

Commit 18b0779

Browse files
authored
Create 156-binary-tree-upside-down.js
1 parent 8dc01dc commit 18b0779

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

156-binary-tree-upside-down.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) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {TreeNode}
11+
*/
12+
const upsideDownBinaryTree = function(root) {
13+
let curr = root
14+
let next = null
15+
let temp = null
16+
let prev = null
17+
while (curr !== null) {
18+
next = curr.left
19+
curr.left = temp
20+
temp = curr.right
21+
curr.right = prev
22+
prev = curr
23+
curr = next
24+
}
25+
return prev
26+
}

0 commit comments

Comments
 (0)