897. 递增顺序搜索树
思路一:dfs + 重新构建一棵树
class Solution {
TreeNode start = new TreeNode();
public void dfs(TreeNode root){
if(root==null)
return;
dfs(root.left);
start.right = new TreeNode(root.val);
start = start.right;
dfs(root.right);
}
public TreeNode increasingBST(TreeNode root) {
TreeNode tmp = start;
dfs(root);
return tmp.right;
}
}
思路二:dfs + 修改原有的树
class Solution {
TreeNode start = new TreeNode();
public void dfs(TreeNode root){
if(root==null)
return;
dfs(root.left);
start.right = root;
start = start.right;
start.left = null;
dfs(root.right);
}
public TreeNode increasingBST(TreeNode root) {
TreeNode tmp = start;
dfs(root);
return tmp.right;
}
}