Concise Java Recursive Solution


  • 4
    M
    	public void flatten(TreeNode root) {
    	if (root == null)
    		return;
    
    	flatten(root.left);
    	flatten(root.right);
    	TreeNode left = root.left;
    	TreeNode right = root.right;
    	root.left = null;
    	root.right = left;
    	while (root.right != null)
    		root = root.right;
    	root.right = right;
    }

Log in to reply
 

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