Java solution Easy to understand

  • 0
    public TreeNode upsideDownBinaryTree(TreeNode root) {
        if(root == null) return root;
        return traverse(root, null, null);
    public TreeNode traverse(TreeNode root, TreeNode parent, TreeNode sibling) {
        if(root == null) return parent;
        TreeNode tmpL = root.left;
        TreeNode tmpR = root.right;
        root.right = parent;
        root.left = sibling;
        return traverse(tmpL, root, tmpR);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.