Simple Accepted java code for Pupplulate next problem.


  • 0
    U
    public class Solution {
    public void connect(TreeLinkNode root) {
    		if (root == null)
    			return;
    
    		TreeLinkNode current = root;
    
    		while (current != null) {
    		    TreeLinkNode nextLevel = current.left;
    			while (current != null) {
    				if (current.left != null) {
    					current.left.next = current.right;
    				}
    				if (current.right != null && current.next != null) {
    					current.right.next = current.next.left;
    				}
    				current = current.next;
    			}
    			current = nextLevel;
    		}
    	}
    
    }

Log in to reply
 

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