Looking for help : My simple Java solution. But Time Limit Exceeded?


  • 0
    D
    public class Solution {
    public TreeNode replace(TreeNode root) {
        if(root.left==null && root.right==null) return root;
        TreeNode t1=(root.left!=null)?replace(root.left):null;
        TreeNode t2=(root.right!=null)?replace(root.right):null;
        if(root.left!=null){
            t1.right=root.right;
            root.right=root.left;
        }
        return (t2==null)?t1:t2;
    }
    
    
    public void flatten(TreeNode root) {
        if(root==null) return;
        replace(root);
    }
    

    }


  • 0
    D

    root.left = nullptr;


Log in to reply
 

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