-
Notifications
You must be signed in to change notification settings - Fork 4
/
tree_nary.go
67 lines (60 loc) · 1.41 KB
/
tree_nary.go
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
package leetcode
import (
"strconv"
"strings"
)
func (n *Node) String() string {
// Leetcode spec
//
// For each visited node, add all children to the queue, then add null
// At the end, strip trailing nulls (if any)
//
if n == nil {
return "[null]"
}
s := func(x int) string {
return strconv.Itoa(x)
}
nodes := []string{s(n.Val), "null"}
queue := []*Node{n}
for i := 0; i < len(queue); i++ {
cur := queue[i]
for _, child := range cur.Children {
nodes = append(nodes, s(child.Val))
queue = append(queue, child)
}
nodes = append(nodes, "null")
}
// Strip trailing nulls
var i int
for i = len(nodes); i > 0 && nodes[i-1] == "null"; i-- {
}
nodes = nodes[:i]
return "[" + strings.Join(nodes, ",") + "]"
}
func ParseNaryTree(tree string) *Node {
strs := strings.Split(tree[1:len(tree)-1], ",")
// Create dummy node in first position
nodes := []*Node{{Val: -1}}
queue := []int{0}
for i, j := 0, 0; i < len(strs) && j < len(queue); i, j = i+1, j+1 {
cur := nodes[queue[j]]
for i < len(strs) && strs[i] != "null" {
// Add child to current node and to the queue
v, _ := strconv.Atoi(strs[i])
nodes = append(nodes, &Node{Val: v})
idx := len(nodes) - 1
cur.Children = append(cur.Children, nodes[idx])
queue = append(queue, idx)
i++
}
}
if len(nodes[0].Children) == 0 {
return nil
}
return nodes[0].Children[0]
}
type Node struct {
Val int
Children []*Node
}