Imagine you have a binary tree and someone creates an exact structural copy of it - same values, same connections, but completely different node objects in memory. Now, you have a reference to a specific node in the original tree, and you need to find the corresponding node (same position, same value) in the cloned tree.
Given:
original: The original binary treecloned: An exact structural copy of the original treetarget: A reference to a specific node in the original tree
Goal: Return a reference to the corresponding node in the cloned tree that has the same value and position as the target node.
Note: You cannot modify any trees or nodes, and you must return an actual node reference from the cloned tree, not just the value.
Input & Output
Visualization
Time & Space Complexity
In worst case, target could be the last node visited, requiring traversal of all n nodes
Recursion stack depth equals tree height h (O(log n) for balanced trees, O(n) for skewed trees)
Constraints
- The number of nodes in the tree is in the range [1, 104]
- The values of the nodes of the tree are unique
- -108 โค Node.val โค 108
- target node is a node from the original tree and is not null