[LeetCode] 1485. Clone Binary Tree With Random Pointer
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 representingNode.val
random_index
: the index of the node (in the input) where the random pointer points to, ornull
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.
Constraints:
- The number of nodes in the
tree
is in the range[0, 1000].
- Each node's value is between
[1, 10^6]
.
克隆含随机指针的二叉树。
给你一个二叉树,树中每个节点都含有一个附加的随机指针,该指针可以指向树中的任何节点或者指向空(null)。
请返回该树的 深拷贝 。
该树的输入/输出形式与普通二叉树相同,每个节点都用 [val, random_index] 表示:
val:表示 Node.val 的整数
random_index:随机指针指向的节点(在输入的树数组中)的下标;如果未指向任何节点,则为 null 。
该树以 Node 类的形式给出,而你需要以 NodeCopy 类的形式返回克隆得到的树。NodeCopy 类和Node 类定义一致。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/clone-binary-tree-with-random-pointer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题目给的是一个二叉树的根节点。在这个二叉树里面,树的节点有可能有一个random指针指向树中一个随机的其他节点。请对这个树做深度复制。
既然是深度复制,又是树的遍历,所以比较直观的感受是用 BFS 或者 DFS 做,在遍历树的每个节点的同时,将每个节点复制,用 hashmap 储存。
首先是BFS。还是常规的层序遍历的思路去遍历树的每个节点,但是注意在做层序遍历的时候,只需要将左孩子和右孩子加入queue进行下一轮遍历,不需要加入random节点。
时间O(n)
空间O(n)
Java实现
1 /** 2 * Definition for Node. 3 * public class Node { 4 * int val; 5 * Node left; 6 * Node right; 7 * Node random; 8 * Node() {} 9 * Node(int val) { this.val = val; } 10 * Node(int val, Node left, Node right, Node random) { 11 * this.val = val; 12 * this.left = left; 13 * this.right = right; 14 * this.random = random; 15 * } 16 * } 17 */ 18 19 class Solution { 20 public NodeCopy copyRandomBinaryTree(Node root) { 21 // corner case 22 if (root == null) { 23 return null; 24 } 25 // normal case 26 Queue<Node> queue = new LinkedList<>(); 27 HashMap<Node, NodeCopy> map = new HashMap<>(); 28 queue.offer(root); 29 map.put(root, new NodeCopy(root.val)); 30 while (!queue.isEmpty()) { 31 Node cur = queue.poll(); 32 NodeCopy copy = map.get(cur); 33 if (cur.random != null) { 34 if (!map.containsKey(cur.random)) { 35 map.put(cur.random, new NodeCopy(cur.random.val)); 36 } 37 copy.random = map.get(cur.random); 38 } 39 if (cur.left != null) { 40 if (!map.containsKey(cur.left)) { 41 map.put(cur.left, new NodeCopy(cur.left.val)); 42 } 43 copy.left = map.get(cur.left); 44 queue.offer(cur.left); 45 } 46 if (cur.right != null) { 47 if (!map.containsKey(cur.right)) { 48 map.put(cur.right, new NodeCopy(cur.right.val)); 49 } 50 copy.right = map.get(cur.right); 51 queue.offer(cur.right); 52 } 53 } 54 return map.get(root); 55 } 56 }
其次是DFS,有那么一点前序遍历的感觉,先复制当前节点,然后再递归处理左孩子右孩子和random节点。
时间O(n)
空间O(n)
Java实现
1 /** 2 * Definition for Node. 3 * public class Node { 4 * int val; 5 * Node left; 6 * Node right; 7 * Node random; 8 * Node() {} 9 * Node(int val) { this.val = val; } 10 * Node(int val, Node left, Node right, Node random) { 11 * this.val = val; 12 * this.left = left; 13 * this.right = right; 14 * this.random = random; 15 * } 16 * } 17 */ 18 19 class Solution { 20 HashMap<Node, NodeCopy> map = new HashMap<>(); 21 22 public NodeCopy copyRandomBinaryTree(Node root) { 23 // corner case 24 if (root == null) { 25 return null; 26 } 27 28 // normal case 29 if (map.containsKey(root)) { 30 return map.get(root); 31 } 32 33 NodeCopy copy = new NodeCopy(root.val); 34 map.put(root, copy); 35 copy.left = copyRandomBinaryTree(root.left); 36 copy.right = copyRandomBinaryTree(root.right); 37 copy.random = copyRandomBinaryTree(root.random); 38 return copy; 39 } 40 }
相关题目
138. Copy List with Random Pointer