LintCode 3690 Clone Binary Tree II (二叉树构建好题)

3690 · Clone Binary Tree II
Algorithms
Medium
Accepted Rate
64%

Description
Solution3
Notes
Discuss
Leaderboard
Record

Description
You are given a binary tree where each node in the tree contains an additional random pointer that can point to any node in the tree or to null.

Please return a deep copy of the tree.

Deep copy

A reference object consists, in general, of two parts: a named Handle, which is what we call a declaration (e.g., a variable), and an internal (unnamed) object, which is the internal object named Handle. It is allocated in the Manged Heap and is typically created by the New method of the newly referenced object.

A deep copy is one in which the source and copy objects are independent of each other, so that changes to either object do not affect the other.

The input form of the tree is the same as a normal binary tree, with each node represented by [val, randomIndex]:

val: Represents the value of the node
randomIndex: The subscript of the node (in the input tree array) pointed to by the random pointer; null if it does not point to any node
The tree is given in the form of the RandomTreeNode class, and similarly you need to return the cloned obtained tree in the form of the RandomTreeNode class.
The range of the number of nodes in a binary tree is
[
0
,
1000
]
[0,1000].

The range of values for each node is
[
1
,
1
0
6
]
[1,10
6
].

Example
Unlike the input form, the output format for each RandomTreeNode node is [val, randomVal], i.e. the second bit of the array is the value of the random node. If you return the correct clone tree object, the output will be as shown in the examples below.

Example 1

Input

root = [[1,null],#,[4,3],[7,0]]
Output

[[1,null],#,[4,3],[7,0]]
Explanation

The initial binary tree is [1,null,4,7].
The random pointer of node 1 points to null, so it is [1, null].
The random pointer of node 4 points to 7, so it is [4, 7].
The random pointer of node 7 points to 1, so it is [7, 1].
Example 2

Input

root = [[1,4],#,[1,0],#,[1,5],[1,5]]
Output

[[1,1],#,[1,1],#,[1,1],[1,1]]
Explanation

A random pointer to a node can point to itself.
Example 3

Input

root = [[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[7,0]]
Output

[[1,7],[2,6],[3,5],[4,4],[5,3],[6,2],[7,1]]
Tags
Related Problems

375
Clone Binary Tree
Medium

3693
Clone N-branch Tree
Medium

解法1:用map把<origNode, copyNode>保存下来。然后构建copy二叉树,如果碰到某个node的copyNode已经在map中就直接返回,否则就构建这个copyNode并将其加入map。

/**
 * Definition of RandomTreeNode:
 * class RandomTreeNode {
 * public:
 *     int val;
 *     RandomTreeNode *left, *right, *random;
 *     RandomTreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = this->random = nullptr;
 *     }
 *     RandomTreeNode(int val, RandomTreeNode* random) {
 *         this->val = val;
 *         this->random = random;
 *         this->left = this->right = nullptr;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: The root node of a binary tree.
     * @return: The cloned tree.
     */
    RandomTreeNode* cloneRandomTree(RandomTreeNode *root) {
        if (!root) return NULL;
        if (um.find(root) != um.end()) {
            return um[root];
        }
        RandomTreeNode *copyRoot = new RandomTreeNode(root->val);
        um[root] = copyRoot;
        copyRoot->left = cloneRandomTree(root->left);
        copyRoot->right = cloneRandomTree(root->right);
        copyRoot->random = cloneRandomTree(root->random);
        return copyRoot;
    }
private:
    unordered_map<RandomTreeNode *, RandomTreeNode *> um; //<orig, copy>
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值