250ms Java Recursion Solution


  • 0
    S
    public class Solution {
    	public void connect(TreeLinkNode root){
    		recursion(root,null);
    	}
    	
    	public void recursion(TreeLinkNode current, TreeLinkNode parent){
    		if(current!=null){
    			recursion(current.left, current);
    			if(parent==null) current.next=null;
    			else if(parent.right!=current) current.next = parent.right;
    			else if(parent.right==current && parent.next!=null) current.next = parent.next.left;
    			else current.next = null;
    			recursion(current.right, current);
    		}
    	}
    }

Log in to reply
 

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