A binary tree is given such that each node contains an additional random pointer which could point to any node in the tree or null.
Return a deep copy of the tree.
The tree is represented in the same input/output way as normal binary trees where each node is represented as a pair of [val, random_index] where:
val: an integer representing Node.valrandom_index: the index of the node (in the input) where the random pointer points to, or null if it does not point to any node.You will be given the tree in class Node and you should return the cloned tree in class NodeCopy. NodeCopy class is just a clone of Node class with the same attributes and constructors.
Example 1:
Input: root = [[1,null],null,[4,3],[7,0]] Output: [[1,null],null,[4,3],[7,0]] Explanation: The original binary tree is [1,null,4,7]. The random pointer of node one is null, so it is represented as [1, null]. The random pointer of node 4 is node 7, so it is represented as [4, 3] where 3 is the index of node 7 in the array representing the tree. The random pointer of node 7 is node 1, so it is represented as [7, 0] where 0 is the index of node 1 in the array representing the tree.
Example 2:
Input: root = [[1,4],null,[1,0],null,[1,5],[1,5]] Output: [[1,4],null,[1,0],null,[1,5],[1,5]] Explanation: The random pointer of a node can be the node itself.
Example 3:
Input: root = [[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[7,0]] Output: [[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[7,0]]
Constraints:
tree is in the range [0, 1000].1 <= Node.val <= 106Problem Overview: You are given a binary tree where every node contains left, right, and an additional random pointer that can point to any node in the tree (or null). The goal is to create a deep copy of the entire structure so that the new tree has identical connections but uses completely new nodes.
Approach 1: Depth-First Search with Hash Map (O(n) time, O(n) space)
The cleanest strategy is to traverse the tree using DFS and maintain a HashMap that maps each original node to its cloned counterpart. When you first encounter a node, create its clone and store the mapping. Then recursively clone the left, right, and random references using the same map so repeated nodes are reused instead of duplicated.
The key insight: the random pointer can point anywhere in the tree, even to nodes that appear later in traversal. The hash map guarantees that every node is created exactly once and reused whenever another pointer references it. Each node is visited a single time, giving O(n) time complexity and O(n) extra space for the map and recursion stack. This pattern appears frequently in pointer-copy problems and combines ideas from Hash Table lookups and Depth-First Search.
Approach 2: Breadth-First Search with Hash Map (O(n) time, O(n) space)
An iterative alternative uses BFS with a queue. Start by cloning the root and storing the mapping from original node to cloned node. While processing nodes from the queue, create clones for their left, right, and random neighbors if they do not exist yet in the map. Each pointer assignment simply references the already-created clone from the hash table.
This approach processes the structure level by level, which some engineers prefer when recursion depth might become large. Every node still enters the queue once, and each pointer assignment is a constant-time hash lookup. Time complexity remains O(n) and the auxiliary space is O(n) due to the map and queue. The traversal mechanics mirror typical Breadth-First Search used on a Binary Tree.
Recommended for interviews: DFS with a hash map is the most common solution interviewers expect. It demonstrates that you recognize the graph-like structure created by the random pointer and avoid duplicate node creation. Mentioning the BFS variant shows you understand that the core idea is the mapping between original and cloned nodes, while traversal order is flexible.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DFS with Hash Map | O(n) | O(n) | Most common solution. Clean recursive implementation for cloning nodes and pointers. |
| BFS with Hash Map | O(n) | O(n) | When you prefer iterative traversal or want to avoid recursion depth limits. |
DFS | Leetcode 1485 | Clone Binary Tree With Random Pointer • Imran Sarwar • 4,448 views views
Watch 6 more video solutions →Practice Clone Binary Tree With Random Pointer with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor