-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtask.go
More file actions
47 lines (36 loc) · 710 Bytes
/
task.go
File metadata and controls
47 lines (36 loc) · 710 Bytes
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
package main
// Node
// TODO закомментить перед отправкой
type Node struct {
value int
left *Node
right *Node
size int
}
func split(node *Node, k int) (*Node, *Node) {
if node == nil {
return nil, nil
}
ls, rs := TreeSizes(node.left, node.right)
if ls+1 > k {
ln, rn := split(node.left, k)
_, rnSize := TreeSizes(ln, rn)
node.size = node.size - ls + rnSize
node.left = rn
return ln, node
}
ln, rn := split(node.right, k-(ls+1))
lnSize, _ := TreeSizes(ln, rn)
node.size = node.size - rs + lnSize
node.right = ln
return node, rn
}
func TreeSizes(l, r *Node) (ls, rs int) {
if l != nil {
ls = l.size
}
if r != nil {
rs = r.size
}
return
}