- Simple dfs traversal.
- check if original node is null if yes then return null.
- if original node is equal to target node then return the cloned node.
- else recur ot left and right of the original and cloned subtrees and store the result in the left and right.
- if left is not null return left else return right.
class Solution {
public:
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target)
{
if (original == NULL)
return NULL;
if (original == target)
return cloned;
TreeNode* left = getTargetCopy(original->left, cloned->left, target);
TreeNode* right = getTargetCopy(original->right, cloned->right, target);
return left != NULL ? left : right;
}
};