Java accepted solution O(1) space


  • 1
    S
    public void connect(TreeLinkNode root) {
        TreeLinkNode node = root;
        while (root != null && root.left != null) {
            while (node != null) {
                node.left.next = node.right;
                if (node.next != null)
                    node.right.next = node.next.left;
                node = node.next;
            }
            
            node = root.left;
            root = node;
        }
    }

Log in to reply
 

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