Practice/Meta/Leetcode 1485. Clone Binary Tree With Random Pointer
CodingOptional
You are given the root of a binary tree where each node contains:
left pointer to the left child (or null)right pointer to the right child (or null)random pointer that can point to any node in the tree (or null)Your task is to create a deep copy of this tree. The cloned tree must be a completely independent copy where:
Return the root of the cloned tree.
[0, 1000]-10^6 and 10^6Example 1:
Input: root = [1, null, null, null] Node 1 with random pointing to itself Output: Cloned tree with one node, random pointing to the cloned node itself Explanation: The single node is cloned and its random pointer correctly references the cloned node
Example 2:
Input: root = [1, 2, 3, null, null, null, null] Tree structure: 1 / \ 2 3 Random pointers: 1->3, 2->1, 3->null Output: Cloned tree with same structure Cloned node 1's random points to cloned node 3 Cloned node 2's random points to cloned node 1 Cloned node 3's random is null Explanation: All structural and random relationships are preserved in the clone
Example 3:
Input: root = null Output: null Explanation: An empty tree clones to an empty tree