https://leetcode.com/articles/increasing-order-search-tree/
https://leetcode.com/problems/increasing-order-search-tree/discuss/165885/C%2B%2BJavaPython-Self-Explained-5-line-O(N)
https://www.codetd.com/article/3154023
Given a 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 9 Output: [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
Note:
- The number of nodes in the given tree will be between 1 and 100.
- Each node will have a unique integer value from 0 to 1000.
https://zxi.mytechroad.com/blog/tree/leetcode-897-increasing-order-search-tree/
修改指向的方式其实比较简单,使用prev指针一直指向了构造出来的这个新树的最右下边的节点,在中序遍历过程中把当前节点的左指针给设置为None,然后把当前节点放到新树的右下角,这样类似于一个越来越长的链表的构建过程。
private TreeNode prev;
public TreeNode increasingBST(TreeNode root) {
TreeNode dummy = new TreeNode(0);
this.prev = dummy;
inorder(root);
return dummy.right;
}
private void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
this.prev.right = root;
this.prev = root;
this.prev.left = null;
inorder(root.right);
}
We can perform the same in-order traversal as in Approach 1. During the traversal, we'll construct the answer on the fly, reusing the nodes of the given tree by cutting their left child and adjoining them to the answer.
Space Complexity: in additional space complexity, where is the height of the given tree, and the size of the implicit call stack in our in-order traversal.
TreeNode cur;
public TreeNode increasingBST(TreeNode root) {
TreeNode ans = new TreeNode(0);
cur = ans;
inorder(root);
return ans.right;
}
public void inorder(TreeNode node) {
if (node == null)
return;
inorder(node.left);
node.left = null;
cur.right = node;
cur = node;
inorder(node.right);
}
https://leetcode.com/problems/increasing-order-search-tree/discuss/165885/C%2B%2BJavaPython-Self-Explained-5-line-O(N)
I didn't use this condition of BST, and just inorder output the whole tree.
Straigh forward idea:
res = inorder(root.left) + root + inorder(root.right)
Several tips here:
- I pass a tail part to the function, so it can link it to the last node.
This operation takesO(1)
, instead ofO(N)
.
Otherwise the whole time complexity will beO(N^2)
. - Also, remember to set
root.left = null
.
Otherwise it will be TLE for Leetcode to traverse your tree. - Should arrange the old tree, not create a new tree.
The judgement won't take it as wrong answer, but it is.
public TreeNode increasingBST(TreeNode root) {
return increasingBST(root, null);
}
public TreeNode increasingBST(TreeNode root, TreeNode tail) {
if (root == null) return tail;
TreeNode res = increasingBST(root.left, root);
root.left = null;
root.right = increasingBST(root.right, tail);
return res;
}
Once we have traversed all the nodes in increasing order, we can construct new nodes using those values to form the answer.
- Time Complexity: , where is the number of nodes in the given tree.
- Space Complexity: , the size of the answer.
public TreeNode increasingBST(TreeNode root) {
List<Integer> vals = new ArrayList();
inorder(root, vals);
TreeNode ans = new TreeNode(0), cur = ans;
for (int v : vals) {
cur.right = new TreeNode(v);
cur = cur.right;
}
return ans.right;
}
public void inorder(TreeNode node, List<Integer> vals) {
if (node == null)
return;
inorder(node.left, vals);
vals.add(node.val);
inorder(node.right, vals);
}
https://www.codetd.com/article/3154023
public TreeNode increasingBST(TreeNode root) {
List<Integer> order = new ArrayList<>();
Stack<TreeNode> s = new Stack<>();
while(root != null || !s.isEmpty()){
while(root != null){
s.push(root);
root = root.left;
}
TreeNode node = s.pop();
order.add(node.val);
if(node.right != null){
root = node.right;
}
}
TreeNode newRoot = null;
TreeNode p = null;
Iterator<Integer> iter = order.iterator();
while(iter.hasNext()){
if(newRoot == null){
newRoot = new TreeNode(iter.next());
p = newRoot;
}
else{
p.right = new TreeNode(iter.next());
p = p.right;
}
}
return newRoot;
}