My Java Recursion Code. concise~ Any optimization?


  • -1
    L
    public void connect(TreeLinkNode root) {
        connectHelper(root, null);
    }
    private void connectHelper(TreeLinkNode root, TreeLinkNode sibling){
        if(root == null)  
            return;
        else 
            root.next = sibling;
            
        connectHelper(root.left,root.right);
        if(sibling != null){
            connectHelper(root.right,sibling.left); 
            connectHelper(sibling.left,sibling.right); 
        }
    }

  • 0
    C

    Any recursive solution will require O(n) extra space...


Log in to reply
 

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