Simple Java solution. Duble recursion.


  • 0
    P

    In this solution priority of traverce plays matter. See connect(TreeLinkNode left, TreeLinkNode right) method.

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

Log in to reply
 

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