```
public class Solution {
public void flatten(TreeNode root) {
//Pre Order
flatten(root, null);
}
public TreeNode flatten(TreeNode root, TreeNode pre){
if(root==null) return null;
if(root.left==null && root.right == null){
//if is leaf node
root.left = pre;
return root;
}
else if(root.left!=null && root.right == null){
root.right = root.left;
root.left = pre;
return flatten(root.right, root);
}
else if(root.left==null && root.right!=null){
root.left = pre;
return flatten(root.right,root);
}else{
//Intermediate Node
TreeNode rightNode = root.right;
root.right = root.left;
root.left = pre;
TreeNode leftTree = flatten(root.right, root);
leftTree.right = rightNode;
return flatten(rightNode, leftTree);
}
}
}
```

I've been thinking it all night... why did I get MLE? can anybody help me figure it out?