Skip to main content

1379. 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/

Python

class Solution:
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
path = self.generate_path(original, target, [])

cur = cloned
for edge in path:
if edge == 'R':
cur = cur.right
else:
cur = cur.left
return cur

def generate_path(self, node: TreeNode, target: TreeNode, path: list) -> Optional[list]:
if not node:
return

if node == target:
return path

left = self.generate_path(node.left, target, ['L'])
right = self.generate_path(node.right, target, ['R'])

if left is not None or right is not None:
return path + left if left else path + right