-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0897-increasing-order-search-tree.js
More file actions
38 lines (34 loc) · 992 Bytes
/
0897-increasing-order-search-tree.js
File metadata and controls
38 lines (34 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 897. Increasing Order Search Tree
* https://leetcode.com/problems/increasing-order-search-tree/
* Difficulty: Easy
*
* Given the root of 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 one
* right child.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var increasingBST = function(root) {
const result = new TreeNode(0);
let current = result;
inorderTraversal(root);
return result.right;
function inorderTraversal(node) {
if (!node) return;
inorderTraversal(node.left);
current.right = new TreeNode(node.val);
current = current.right;
inorderTraversal(node.right);
}
};