-
Notifications
You must be signed in to change notification settings - Fork 0
/
increasing-order-search-tree.js
45 lines (43 loc) · 1.14 KB
/
increasing-order-search-tree.js
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
39
40
41
42
43
44
45
/**
* 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) {
// 1. 中序遍历 直接改变节点指向
const dummyNode = new TreeNode(-1);
let current = dummyNode;
const inOrderTraverse = (node) => {
if (node) {
inOrderTraverse(node.left);
current.right = node;
// 左节点置空
node.left = null;
current = node;
inOrderTraverse(node.right);
}
};
inOrderTraverse(root);
return dummyNode.right;
// 2. 中序遍历 生成新节点
// const dummyNode = new TreeNode(-1);
// let current = dummyNode;
// const inOrderTraverse = (node) => {
// if (node) {
// inOrderTraverse(node.left);
// current.right = new TreeNode(node.val);
// current = current.right;
// inOrderTraverse(node.right);
// }
// };
// inOrderTraverse(root);
// return dummyNode.right;
};