[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 representing Node.val
  • random_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 NodeCopyNodeCopy 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 class Solution {
 2     public NodeCopy copyRandomBinaryTree(Node root) {
 3         // normal case
 4         if (root == null) {
 5             return null;
 6         }
 7 
 8         // normal case
 9         HashMap<Node, NodeCopy> map = new HashMap<>();
10         map.put(root, new NodeCopy(root.val));
11         Queue<Node> queue = new LinkedList<>();
12         queue.add(root);
13         while (!queue.isEmpty()) {
14             Node node = queue.poll();
15             NodeCopy newNode = map.get(node);
16             if (node.random != null) {
17                 if (!map.containsKey(node.random)) {
18                     map.put(node.random, new NodeCopy(node.random.val));
19                 }
20                 newNode.random = map.get(node.random);
21             }
22             if (node.left != null) {
23                 if (!map.containsKey(node.left)) {
24                     map.put(node.left, new NodeCopy(node.left.val));
25                 }
26                 newNode.left = map.get(node.left);
27                 queue.add(node.left);
28             }
29             if (node.right != null) {
30                 if (!map.containsKey(node.right)) {
31                     map.put(node.right, new NodeCopy(node.right.val));
32                 }
33                 newNode.right = map.get(node.right);
34                 queue.add(node.right);
35             }
36         }
37         return map.get(root);
38     }
39 }

其次是DFS,有那么一点前序遍历的感觉,先复制当前节点,然后再递归处理左孩子右孩子和random节点。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     HashMap<Node, NodeCopy> map = new HashMap<>();
 3 
 4     public NodeCopy copyRandomBinaryTree(Node root) {
 5         // corner case
 6         if (root == null) {
 7             return null;
 8         }
 9 
10         // normal case
11         if (map.containsKey(root)) {
12             return map.get(root);
13         }
14 
15         NodeCopy copy = new NodeCopy(root.val);
16         map.put(root, copy);
17         copy.left = copyRandomBinaryTree(root.left);
18         copy.right = copyRandomBinaryTree(root.right);
19         copy.random = copyRandomBinaryTree(root.random);
20         return copy;
21     }
22 }

相关题目

133. Clone Graph

138. Copy List with Random Pointer

1485. Clone Binary Tree With Random Pointer

1490. Clone N-ary Tree

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/13557573.html