8-line concise java solution, O(n) time, O(1) space, 1ms


  • 0
    M
        public void connect(TreeLinkNode head) {
        	while(head != null){
        		TreeLinkNode nextHead = head.left;
        		for(TreeLinkNode node = head; node!=null; node = node.next){
        			if(node.left!=null) node.left.next = node.right;
        			if(node.right!=null && node.next!=null) node.right.next = node.next.left;
        		}
        		head = nextHead;
        	}
        }
    

Log in to reply
 

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