最终结果的形式是,node.left变为根,node.right变为左节点,node变为右节点
public class test {
public TreeNode upsideDownBinaryTree(TreeNode root){
if(root == null){
return root;
}
if(root.left == null && root.right == null){
return root;
}
TreeNode trueroot = upsideDownBinaryTree(root.left);
root.left.left = root.right;
root.left.right = root;
root.left = null;
root.right = null;
return trueroot;
}
}
取出的trueroot并未做任何的操作,只是为了取到最左边的节点作为真正的根节点最终返回。
在递归的过程中,完成改变结构的操作。