0ms Java Recursive code with comments. You can't beat this one.


  • 1
    P

    The solution cannot get more efficient than this.

    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
            
        if(root.left != null){
            root.left.next = root.right; //Connect left and right,
            if(root.next != null) // go to next.
                root.right.next = root.next.left; // connect right and next subtree.
        }
        
        connect(root.left); // recurse on both child.
        connect(root.right);
    }
    

Log in to reply
 

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