-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path114.js
58 lines (55 loc) · 1.64 KB
/
114.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
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 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 {void} Do not return anything, modify root in-place instead.
*/
var flatten = function(root) {
if (root) {
flatten(root.left)
flatten(root.right)
let leftTree = new TreeNode(0)
let rightTree = new TreeNode(0)
leftTree = root.left
rightTree = root.right
root.left = null
root.right = leftTree
let tmp = new TreeNode(0)
tmp = root
while (tmp.right) {
tmp = tmp.right
}
tmp.right = rightTree
return root
}
return null
};
/*
2021/8/6
96% 11%
本质上是二叉树的前序遍历
空间复杂度O1的方法是
var flatten = function(root) {
let curr = root;
while (curr !== null) {
if (curr.left !== null) {
const next = curr.left;
let predecessor = next;
while (predecessor.right !== null) {
predecessor = predecessor.right;
}
predecessor.right = curr.right;
curr.left = null;
curr.right = next;
}
curr = curr.right;
}
};
对树进行原地的更改,在遍历的过程中通过近似旋转的方式找到每个节点的前驱(左子树最右边)的节点,将右子树移动到前驱下方,再将左子树移到右子树位置
*/