非前序遍历的方法
class Solution {
public void flatten(TreeNode root) {
connect(root);
}
public TreeNode connect(TreeNode root){
if(root.left==null){
if(root.right==null)
return root;
else return connect(root.right);
}else{
if(root.right==null){
root.right = root.left;
root.left = null;
return connect(root.right);
}else{
TreeNode left = connect(root.left);
TreeNode right = connect(root.right);
left.right = root.right;
root.right = root.left;
root.left = null;
return right;
}
}
}
}