forked from mrekucci/epi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtorder.go
40 lines (37 loc) · 1.09 KB
/
btorder.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
// Copyright (c) 2015, Peter Mrekaj. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.txt file.
package queues
// IntBTree represents a binary tree with int key.
type IntBTree struct {
Data int
left *IntBTree
right *IntBTree
}
// DepthOrder returns a slice consisting of keys belonging to the
// same level (order is from left to right) of the binary tree t.
// The time complexity is O(n). The O(m) additional space is
// needed, where m is the maximum number of nodes at any level.
func DepthOrder(t *IntBTree) (order [][]int) {
var level []int
pq := new(arrayQueue)
pq.Enqueue(t) // Add root.
cnt := pq.Len() // Number of elements on the same level.
for pq.Len() != 0 {
n := pq.Dequeue().(*IntBTree)
cnt--
if n != nil {
pq.Enqueue(n.left)
pq.Enqueue(n.right)
level = append(level, n.Data)
}
if cnt == 0 {
cnt = pq.Len() // Set count to the number of elements that should be processed on next level.
if len(level) != 0 {
order = append(order, level)
level = []int(nil)
}
}
}
return order
}