javascript constant space use dummyheader


  • 0
    Y
    /**
     * 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) {
        //link list
        var runner=root,nextRunnerHeader=null;
        while(runner!==null)
        {
            var dummyH = new TreeLinkNode(0);var dummy=dummyH;
            while(runner!==null)
            {
                if(runner.left!==null)
                    {dummy.next=runner.left;dummy=dummy.next; }
                if(runner.right!==null)
                    {dummy.next=runner.right;dummy=dummy.next; }
                runner=runner.next;
            }
            //dummyheader never changed, so return;
            if(dummy===dummyH) return;
            runner=dummyH;
        }
    };

Log in to reply
 

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