---
# System prepended metadata

title: 1485. Clone Binary Tree With Random Pointer
tags: [Leetcode, FaceBook, Deep Copy, DFS, Medium]

---

# 1485. Clone Binary Tree With Random Pointer
###### tags: `Leetcode` `FaceBook` `Medium` `Deep Copy` `DFS`
Link: https://leetcode.com/problems/clone-binary-tree-with-random-pointer/
## 思路
和其他的深度拷贝问题都差不多~
## Code
```java=
class Solution {
    public NodeCopy copyRandomBinaryTree(Node root) {
        Map<Node, NodeCopy> copyMap = new HashMap<>();
        NodeCopy rootCopy = copy(root, copyMap);
        return rootCopy;
    }
    public NodeCopy copy(Node root, Map<Node, NodeCopy> copyMap){
        if(root == null) return null;
        if(copyMap.containsKey(root)) return copyMap.get(root);
        NodeCopy rootCopy = new NodeCopy(root.val);
        copyMap.put(root, rootCopy);
        if(root.left!=null) rootCopy.left = copy(root.left, copyMap);
        if(root.right!=null) rootCopy.right = copy(root.right, copyMap);
        if(root.random!=null) rootCopy.random = copy(root.random, copyMap);
        return copyMap.get(root);
    }
}
```