Concise solution in Java.


  • 3
    P

    Get left and right children. Each iteration keep to the right on left child and to the left on right child and do connection. I think that code is clear enought to understand.

    public void connect(TreeLinkNode root) {
    	if (root == null) return;
    
    	TreeLinkNode left = root.left;
    	TreeLinkNode right = root.right;
        
        connect(left);
        connect(right);
        
        while (left != null) {
        	left.next = right;
        	left = left.right;
        	right = right.left;
        }
    }

Log in to reply
 

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