1. Code
class Solution:
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
q = deque([(original, cloned)]) # Store the original and cloned nodes together.
while q:
origin, clon = q.popleft()
if origin is target: # When the target node is found, returns the cloned node.
return clon
if origin.left:
q.append((origin.left, clon.left))
if origin.right:
q.append((origin.right, clon.right))
2. Result
Runtime : 596 ms(96.15%), Memory usage : 24 MB(82.41%)
(Runtime can be different by a system even if it is a same code.)