Increasing Order Search Tree
Description
Given a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.
Example 1: Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]5 / \ 3 6
/ \
2 4 8 / / \ 1 7 9Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
1
2
3
4
5
6
7
8
9
Constraints:
- The number of nodes in the given tree will be between
1
and100
. - Each node will have a unique integer value from
0
to1000
.
Solution(javascript)
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
const increasingBST = (root) => {
const vals = []
const aux = (node) => {
if (!node) {
return
}
if (node.left) {
aux(node.left)
}
vals.push({ val: node.val, left: null, right: null })
if (node.right) {
aux(node.right)
}
}
aux(root)
for (let i = 0; i < vals.length - 1; i++) {
vals[i].right = vals[i + 1]
}
return vals[0]
}