解题思路
中序遍历后再建立一个新的二叉树即可
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
TreeNode ans;
TreeNode temp;
public TreeNode increasingBST(TreeNode root) {
ans = new TreeNode();
ans.left=null;
temp = ans;
dfs(root);
return ans.right;
}
public void dfs(TreeNode root){
if (root==null) return;
if(root.left!=null){
dfs(root.left);
}
TreeNode node = new TreeNode(root.val);
temp.right=node;
temp=temp.right;
if (root.right!=null){
dfs(root.right);
}
}
}