Leetcode 1498 Solution

This article provides solution to leetcode question 1498 (find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree)

https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree

Solution

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
        if not original:
            return None

        if id(original) == id(target):
            return cloned

        left_target = self.getTargetCopy(original.left, cloned.left, target)
        if left_target is not None:
            return left_target

        right_target = self.getTargetCopy(original.right, cloned.right, target)
        if right_target is not None:
            return right_target