给你一个树,请你 按中序遍历 重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有一个右子结点。
示例 :
输入:[5,3,6,2,4,null,8,1,null,null,null,7,9]
5
/ \
3 6
/ \ \
2 4 8
/ / \
1 7 9
输出:[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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode increasingBST(TreeNode root) {
//left root right
List<Integer> list = new ArrayList<>();
inorder(root,list);
TreeNode node = new TreeNode(-1);
TreeNode current = node;
for(Integer t:list){
TreeNode tempt = new TreeNode(t);
tempt.left = null;
current.right = tempt;
current = current.right;
}
return node.right;
}
public void inorder(TreeNode root,List<Integer> list){
if(root == null)
return;
inorder(root.left,list);
System.out.println("val=="+root.val);
list.add(root.val);
inorder(root.right,list);
}
}