Simple recursive solution


  • 0
    L
    /**
     * Definition for binary tree with next pointer.
     * function TreeLinkNode(val) {
     *     this.val = val;
     *     this.left = this.right = this.next = null;
     * }
     */
    
    /**
     * @param {TreeLinkNode} root
     * @return {void} Do not return anything, modify tree in-place instead.
     */
    var connect = function(root) {
        if(root == null) return;
        var pLeft = root.left;
        var pRight = root.right;
        connect(pLeft);
        connect(pRight);
        while(pLeft !== null) {
            pLeft.next = pRight;
            pLeft = pLeft.right;
            pRight = pRight.left;
        }
    };
    

Log in to reply
 

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