Problem
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.
Examples
Example 1:
graph LR subgraph Original H(7) --- E(4) H(7) --- D(3):::green D(3) --- F(6) D(3) --- T(19) end subgraph Cloned H'(7) --- E'(4) H'(7) --- D'(3):::yellow D'(3) --- F'(6) D'(3) --- T'(19) end Original ~~~ Cloned classDef green fill:#3CB371,stroke:#000,stroke-width:1px,color:#fff; classDef yellow fill:#FFD700,stroke:#000,stroke-width:1px,color:#000;
|
|
Example 2:
graph LR subgraph Original H(7):::green end subgraph Cloned H'(7):::yellow end Original ~~~ Cloned classDef green fill:#3CB371,stroke:#000,stroke-width:1px,color:#fff; classDef yellow fill:#FFD700,stroke:#000,stroke-width:1px,color:#000;
|
|
Example 3:
graph LR subgraph Original H(8) H(8) ~~~ N1:::hidden H --- G(6) G(6) ~~~ N2:::hidden G --- F(5) F(5) ~~~ N2:::hidden F --- E(4):::green E(4) ~~~ N3:::hidden E --- D(3) D(3) ~~~ N4:::hidden D --- C(2) C(2) ~~~ N5:::hidden C --- B(1) end subgraph Cloned H'(8) H'(8) ~~~ N1':::hidden H' --- G'(6) G'(6) ~~~ N2':::hidden G' --- F'(5) F'(5) ~~~ N2':::hidden F' --- E'(4):::yellow E'(4) ~~~ N3':::hidden E' --- D'(3) D'(3) ~~~ N4':::hidden D' --- C'(2) C'(2) ~~~ N5':::hidden C' --- B'(1) end Original ~~~ Cloned classDef green fill:#3CB371,stroke:#000,stroke-width:1px,color:#fff; classDef yellow fill:#FFD700,stroke:#000,stroke-width:1px,color:#000; classDef hidden display:none
|
|
Follow up
Could you solve the problem if repeated values on the tree are allowed?
Solution
We can do both BFS and DFS. With DFS, we can easily handle the follow up as well.
Method 1 - BFS
Instead of traversing the trees recursively (DFS), we can use an iterative approach with Breadth-First Search (BFS). BFS utilises a queue to simultaneously traverse both the original
and cloned
trees level-by-level. At each level, check whether the current node in the original
tree matches the target node. If yes, return the corresponding node from the cloned
tree.
BFS Approach
- Use queues to store nodes from the
original
and its corresponding node from thecloned
tree. - Start by adding the roots of both trees to their respective queues.
- Dequeue nodes from the
original
andcloned
queues one-by-one, and:- If the node from the
original
tree matchestarget
, return the corresponding node from thecloned
tree. - Add the left and right children of the dequeued nodes to the respective queues.
- If the node from the
- Repeat until the target node is found.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, as every node is visited once. - 🧺 Space complexity:
O(n)
. The maximum size of the queue can ben/2
(in the worst case when the tree is balanced, the last level has half the nodes).
Method 2 - DFS
We can use DFS traversal on both trees simultaneously and compare the current node from the original
tree with the target
node.
Base case
- If
original
is null, returnnull
, sincecloned
will also be null at that moment. - If
original
matches thetarget
, returncloned
, as it is the corresponding node. Other case - Otherwise, search recursively on both sides to locate the
target
.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, as we may need to visit every node in the binary tree in the worst-case scenario. - 🧺 Space complexity:
O(h)
. The space required is determined by the recursive call stack, whereh
is the height of the tree. For a balanced binary tree,h = O(log n)
; for a skewed tree,h = O(n)
.