*1485 Clone Binary Tree With Random Pointer

https://leetcode.com/problems/clone-binary-tree-with-random-pointer/

solution

class Solution:
    # clone的题目也是在: 构建一棵树.
    # 更好的方式确实也需要保存一个map, 由于random的存在. https://algo.monster/liteproblems/1485
    def clone_random_tree(self, root: RandomTreeNode) -> RandomTreeNode:
        if not root:
            return None

        copy = RandomTreeNode(root.val, None)
        copy.left = self.clone_random_tree(root.left)
        copy.right = self.clone_random_tree(root.right)
        if root.random:
            copy.random = self.clone_random_tree(root.random)
        return copy

时间复杂度:O(n) 空间复杂度:O()

Last updated