-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn_ary_tree_preorder_traversal.go
79 lines (70 loc) · 1.66 KB
/
n_ary_tree_preorder_traversal.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
68
69
70
71
72
73
74
75
76
77
78
79
package problem0589
import . "leetcodedaily/helpers/narytree"
/*
Given the root of an n-ary tree, return the preorder traversal of its nodes' values.
Nary-Tree input serialization is represented in their level order traversal.
Each group of children is separated by the null value (See examples)
*/
func preorder(root *Node) []int {
var res = []int{}
if root == nil {
return res
}
// Appending current value
res = append(res, root.Val)
if root.Children != nil {
// Recursively adding the children to result
for i := range root.Children {
res = append(res, preorder(root.Children[i])...)
}
}
return res
}
func preorderShared(root *Node) []int {
var res []int
if root == nil {
return res
}
// Same as preorder but with only one list
visitChildren(root, &res)
return res
}
func visitChildren(root *Node, res *[]int) {
// Appending current value
(*res) = append(*res, root.Val)
if root.Children != nil {
// Recursively adding the children to result
for i := range root.Children {
visitChildren(root.Children[i], res)
}
}
}
func preorderStack(root *Node) []int {
var res []int
var stack = NodeStack{root}
if root == nil {
return res
}
// Looping until stack is empty
for len(stack) > 0 {
// Adding top value to result
cur := stack.Pop()
res = append(res, cur.Val)
if cur.Children != nil {
// Adding all the children in reverse order to the stack
for i := len(cur.Children) - 1; i >= 0; i-- {
stack.Push(cur.Children[i])
}
}
}
return res
}
type NodeStack []*Node
func (s *NodeStack) Pop() *Node {
ret := (*s)[len(*s)-1]
(*s) = (*s)[:len(*s)-1]
return ret
}
func (s *NodeStack) Push(val *Node) {
(*s) = append((*s), val)
}