Find a Corresponding Node of a Binary Tree in a Clone of That Tree - Problem

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 tree
  • cloned: An exact structural copy of the original tree
  • target: 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

example_1.py โ€” Basic Tree Structure
$ Input: original = [7,4,3,null,null,6,19], cloned = [7,4,3,null,null,6,19], target = 3
โ€บ Output: Reference to node with value 3 in cloned tree
๐Ÿ’ก Note: The target node has value 3 and is the right child of root in original tree. The corresponding node in cloned tree is also the right child of root with value 3.
example_2.py โ€” Single Node Tree
$ Input: original = [1], cloned = [1], target = 1
โ€บ Output: Reference to root node in cloned tree
๐Ÿ’ก Note: When the tree has only one node and that node is the target, the corresponding node is simply the root of the cloned tree.
example_3.py โ€” Deep Tree Structure
$ Input: original = [7,4,3,null,null,6,19], cloned = [7,4,3,null,null,6,19], target = 6
โ€บ Output: Reference to node with value 6 in cloned tree
๐Ÿ’ก Note: The target node with value 6 is located as the left child of node 3. The corresponding node in cloned tree maintains the same structural position.

Visualization

Tap to expand
๐Ÿชž Mirror House NavigationOriginal Room๐Ÿช‘๐Ÿช‘๐Ÿช‘๐Ÿ‘คYouTarget ChairMirror Room๐Ÿช‘๐Ÿช‘๐Ÿช‘๐Ÿ‘คMirror YouFound Chair!โšก Synchronized Movement
Understanding the Visualization
1
Enter Both Rooms
Start at the entrance of both the original room and its mirror copy
2
Walk in Perfect Sync
Take identical steps in both rooms - left turn in original means left turn in mirror
3
Recognize Target Location
When you reach the target chair in original room, you're at corresponding chair in mirror room
4
Mission Complete
Return the chair from the mirror room - it's the exact correspondence you were looking for
Key Takeaway
๐ŸŽฏ Key Insight: By maintaining perfect synchronization between both tree traversals, we guarantee that when we find our target in the original tree, we're automatically positioned at the corresponding node in the cloned tree.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

In worst case, target could be the last node visited, requiring traversal of all n nodes

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack depth equals tree height h (O(log n) for balanced trees, O(n) for skewed trees)

n
2n
โœ“ Linear Space

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
Asked in
Facebook 15 Amazon 12 Google 8 Microsoft 6
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen