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].

最低0.47元/天 解锁文章
884

被折叠的 条评论
为什么被折叠?



