LeetCode-Binary Tree Upside Down

right child is leaf or null 保证了可以flip 就是说right node不可能再有child node

每个node变成自己left child的 right node 自己的right child变成left child的left child

递归 记得要先把左边先做一遍再移动root层的指针 同时root的left right point 要set null

public class Solution {
    public TreeNode upsideDownBinaryTree(TreeNode root) {
        if ( root == null || root.left == null )
            return root;
        TreeNode res = upsideDownBinaryTree ( root.left );
        root.left.left = root.right;
        root.left.right = root;
        root.left = null;
        root.right = null;
        return res;
    }
}


你可能感兴趣的:(LeetCode-Binary Tree Upside Down)