Another simple JavaScript solution

  • 0

    Since it's a full binary tree, our job is much simpler, at each node, connects its left and right child, and try to connect the right child with the left child of node's next. A simple preorder traversal should be able to help us solve this problem.

     * @param {TreeLinkNode} root
     * @return {void} Do not return anything, modify tree in-place instead.
    var connect = function(root) {
        if (!root || !root.left) { // sanity check
        = root.right;                         // connect left -> right = ? : null; // connect right -> next's left

    Time complexity: O(n)
    Space complexity: O(1)

Log in to reply

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