Skip to content

Latest commit

 

History

History
27 lines (23 loc) · 986 Bytes

1379_findACorrespondingNodeOfABinaryTreeInACloneOfThatTree.md

File metadata and controls

27 lines (23 loc) · 986 Bytes

DFS

  • 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.

Code

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;
    }
};