O(1) space solution


  • 0
    Z
    public class Solution {
      public void connect(TreeLinkNode root) {
        if(root==null || root.left==null) return;
        TreeLinkNode head = root.left,current = root,previous = null;
        while(true){
            while(current!=null){
                if(previous != null) previous.next = current.left;
                current.left.next = current.right;
                previous = current.right;
                current = current.next;
            }
            if (head.left == null) break;
            current = head;
            previous = null;
            head = current.left; 
        }
        return;
      }
    }

Log in to reply
 

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