Simple Java solution. Duble recursion.

  • 0

    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, 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 ( == null) = right;

Log in to reply

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