-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1485-CloneBinaryTreeWithRandomPointer.go
More file actions
126 lines (110 loc) · 4.38 KB
/
1485-CloneBinaryTreeWithRandomPointer.go
File metadata and controls
126 lines (110 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
// 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 NodeCopy.
// NodeCopy class is just a clone of Node class with the same attributes and constructors.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/06/17/clone_1.png" />
// 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.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2020/06/17/clone_2.png" />
// Input: root = [[1,4],null,[1,0],null,[1,5],[1,5]]
// Output: [[1,4],null,[1,0],null,[1,5],[1,5]]
// Explanation: The random pointer of a node can be the node itself.
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2020/06/17/clone_3.png" />
// Input: root = [[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[7,0]]
// Output: [[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[7,0]]
// Constraints:
// The number of nodes in the tree is in the range [0, 1000].
// 1 <= Node.val <= 10^6
import "fmt"
type Node struct {
Val int
Left *Node
Right *Node
Random *Node
}
type NodeCopy struct {
Val int
Left *NodeCopy
Right *NodeCopy
Random *NodeCopy
}
/**
* Definition for a Node.
* type Node struct {
* Val int
* Left *Node
* Right *Node
* Random *Node
* }
*/
func copyRandomBinaryTree(root *Node) *NodeCopy {
connects := map[*Node]*NodeCopy{ }
var dfs func(root *Node)
dfs = func(root *Node) {
if root == nil {
return
}
nNode := &NodeCopy{ Val: root.Val }
connects[root] = nNode
dfs(root.Left)
dfs(root.Right)
}
dfs(root)
for old, new := range connects {
new.Left = connects[old.Left]
new.Right = connects[old.Right]
new.Random = connects[old.Random]
}
return connects[root]
}
func copyRandomBinaryTree1(root *Node) *NodeCopy {
if root == nil {return nil }
mem := map[*Node]*NodeCopy{}
var helper func(root *Node) *NodeCopy
helper = func(root *Node) *NodeCopy {
if root == nil { return nil }
if val, ok := mem[root]; ok {
return val
}
t := &NodeCopy{Val: root.Val}
mem[root] = t
t.Left = helper(root.Left)
t.Right = helper(root.Right)
t.Random = helper(root.Random)
return t
}
return helper(root)
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/06/17/clone_1.png" />
// 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.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2020/06/17/clone_2.png" />
// Input: root = [[1,4],null,[1,0],null,[1,5],[1,5]]
// Output: [[1,4],null,[1,0],null,[1,5],[1,5]]
// Explanation: The random pointer of a node can be the node itself.
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2020/06/17/clone_3.png" />
// Input: root = [[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[7,0]]
// Output: [[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[7,0]]
fmt.Println()
}