Java - Vanilla Solution


  • 0

    public class Solution {

    TreeNode res; 
    
    public TreeNode upsideDownBinaryTree(TreeNode root) {
        if (root != null) trans(root); 
        return res;     
         
    }
    
    private TreeNode trans(TreeNode curr){
        if (curr.left == null && curr.right == null) {
            res = curr; 
            return curr; 
        }
        
        TreeNode new_parent = trans(curr.left); 
        if (curr.right != null) new_parent.left = curr.right; 
        new_parent.right = curr; 
    
        curr.left = null; 
        curr.right = null;
    
        return curr; 
    }
    

    }


Log in to reply
 

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