/**
consider this example of 114. Flatten Binary Tree to Linked List, I'm creating new TreeNode from
the root and returning the result in root i can verify the result is correct in debbugger but leetcode does not accept the
answer instead it says the output is original tree. please help me with this.
*/
class Solution {
public void flatten(TreeNode root) {
root = help(root);
}
public TreeNode help(TreeNode root){
if(root == null){
return null;
}
TreeNode r = help(root.right);
TreeNode l = help(root.left);
TreeNode ans = new TreeNode(root.val);
if(r == null && l == null){
}
else if (l == null){
ans.right = r;
}
else if(r == null){
ans.right = l;
}
else{
TreeNode temp = l;
while(temp.right != null){
temp = temp.right;
}
temp.right = r;
ans.right = l;
}
return ans;
}
}