I am trying the LeetCode problem1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree:
Given two binary trees
originalandclonedand given a reference to a nodetargetin the original tree.The
clonedtree is a copy of theoriginaltree.Return a reference to the same node in the cloned tree.
Note that you are not allowed to change any of the two trees or the
targetnode and the answer must be a reference to a node in theclonedtree.
This is my code:
class Solution {
public:
TreeNode* ans = NULL ;
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
// if(cloned == NULL){return cloned ;}
if(cloned == NULL){return cloned ;}
// if(cloned->val == target->val){
// return cloned ;
// }
if(cloned->val == target->val){
ans = cloned ;
}
// return getTargetCopy(original,cloned->left ,target) ;
// return getTargetCopy(original,cloned->right,target) ;
getTargetCopy(original,cloned->left ,target) ;
getTargetCopy(original,cloned->right,target) ;
return ans ;
}
};
The commented out part was my initial code but it returned wrong answer
Your input
[7,4,3,null,null,6,19]
3
Output
null
Expected
3
Help me understand the problem in my code