Problem:
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.
Explanation:
将一个搜索二叉树转换成右斜树。
My Thinking:
前序遍历,建立连接关系即可。
My Solution:
class Solution {
public TreeNode increasingBST(TreeNode root) {
TreeNode newroot=new TreeNode(0);
buildTree(root,newroot);
return newroot.right;
}
public TreeNode buildTree(TreeNode root,TreeNode newroot){
if(root==null)
return newroot;
TreeNode nowroot=buildTree(root.left,newroot);
nowroot.right=new TreeNode(root.val);
nowroot=buildTree(root.right,nowroot.right);
return nowroot;
}
}
Optimum Thinking:
Optimum Solution: