Share my JAVA solution


  • 0
    S
    public static void connect(TreeLinkNode root) {
    	if (root == null) {
    		return;
    	}
    	
    	TreeLinkNode t = root.next;
    	while (t != null) {
    		if (t.left != null) {
    			t = t.left;
    			break;
    		} else if (t.right != null) {
    			t = t.right;
    			break;
    		} else {
    			t = t.next;
    		}
    	}
    	if (root.right != null) {
    		root.right.next = t;
    	}
    	if (root.left != null) {
    		if (root.right != null) {
    			root.left.next = root.right;
    		} else {
    			root.left.next = t;
    		}
    	}
    	connect(root.right);
    	connect(root.left);
    }

Log in to reply
 

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