Java Recursive 1 ms solution


  • 0
    S
    
    	public void flatten(TreeNode root) {
    		if (root == null)
    			return;
    		flatten(root.left);
    		TreeNode cur = root.left;
    		if (cur != null) {
    		  while (cur.right != null) {
    				cur = cur.right;
    			}
    			cur.right = root.right;
    			root.right = root.left;
    			root.left = null;
    			flatten(cur.right);
    		} else
    			flatten(root.right);
    	}
    

Log in to reply
 

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