Given two binary trees original and cloned and given a reference to a node target in the original tree.
The cloned tree is a copy of the original tree.
Return a reference to the same node in the cloned tree.
Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree.
Example 1:
Input: tree = [7,4,3,null,null,6,19], target = 3 Output: 3 Explanation: In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree.
Example 2:
Input: tree = [7], target = 7 Output: 7
Example 3:
Input: tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4 Output: 4
Constraints:
tree is in the range [1, 104].tree are unique.target node is a node from the original tree and is not null.Follow up: Could you solve the problem if repeated values on the tree are allowed?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have two identical family trees. We're trying to find a specific person in the second tree, knowing who they are in the first tree. The brute force approach is simply to check every single person in the second tree until we find the one that corresponds to the person we're looking for in the first tree.
Here's how the algorithm would work step-by-step:
def find_corresponding_node_brute_force(original_root, cloned_root, target_node):
if not original_root or not cloned_root or not target_node:
return None
def path_to_node(root, target):
if not root:
return None
if root == target:
return []
left_path = path_to_node(root.left, target)
if left_path is not None:
return ['L'] + left_path
right_path = path_to_node(root.right, target)
if right_path is not None:
return ['R'] + right_path
return None
target_path = path_to_node(original_root, target_node)
def find_node_by_path(root, path):
if not root:
return None
current_node = root
for direction in path:
# Traverse the tree according to the path
if direction == 'L':
current_node = current_node.left
else:
current_node = current_node.right
if not current_node:
return None
return current_node
# Search for the corresponding node using the computed path
corresponding_node = find_node_by_path(cloned_root, target_path)
return corresponding_nodeThe key is to traverse both trees simultaneously. We explore both the original tree and its clone at the same time, step-by-step, until we find the target node in the original tree. The corresponding node in the cloned tree at that moment is our answer.
Here's how the algorithm would work step-by-step:
def find_corresponding_node(
original_tree, cloned_tree, target_node
):
if not original_tree or not cloned_tree:
return None
queue_original = [original_tree]
queue_cloned = [cloned_tree]
while queue_original:
current_original_node = queue_original.pop(0)
current_cloned_node = queue_cloned.pop(0)
# Check if the current original node is the target.
if current_original_node == target_node:
return current_cloned_node
# Traverse to the next level
if current_original_node.left:
queue_original.append(current_original_node.left)
queue_cloned.append(current_cloned_node.left)
# Traverse to the next level
if current_original_node.right:
queue_original.append(current_original_node.right)
queue_cloned.append(current_cloned_node.right)
return None| Case | How to Handle |
|---|---|
| Both original and cloned trees are null | Return null immediately as there's no corresponding node. |
| Original tree is null, but cloned tree is not (or vice-versa) | Return null since there is no original to match. |
| Target node is the root node | The cloned tree's root should be returned directly if the target is original root. |
| Target node does not exist in the original tree | Return null if target node is not found during tree traversal. |
| Large trees with deep recursion stacks causing stack overflow | Consider an iterative approach (e.g., Breadth-First Search) to avoid stack overflow. |
| Trees with identical structures but different data (not the target node) | Ensure the comparison logic focuses on structure, not the data of other nodes. |
| Memory constraints with very large trees | Optimize the memory footprint of the search, possibly releasing memory used by visited nodes. |
| Cloned tree is not a true clone (structure is different) | The algorithm assumes a true clone, but if the structure differs, the corresponding node may not be found, returning null. |