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

  • 1

    The solution cannot get more efficient than this.

    public void connect(TreeLinkNode root) {
        if(root == null)
        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.

Log in to reply

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