-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_width.go
65 lines (57 loc) · 1.65 KB
/
tree_width.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
package problem0662
import . "leetcodedaily/helpers/binarytree"
/*
Given the root of a binary tree, return the maximum width of the given tree.
The maximum width of a tree is the maximum width among all levels.
The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes),
where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level
are also counted into the length calculation.
It is guaranteed that the answer will in the range of a 32-bit signed integer.
*/
type pair struct {
T *TreeNode
V int
}
func widthOfBinaryTree(root *TreeNode) int {
// Res is the max width
var res int = 1
// Queue of pair for bfs
var queue = []pair{{root, 0}}
for len(queue) > 0 {
var cnt = len(queue)
// Start and end are the opposite sides of each level
var start, end = peek(queue)
// So the width is the difference between them
res = max(res, end.V-start.V+1)
for i := 0; i < cnt; i++ {
var p, _ = peek(queue)
var idx = p.V - start.V
queue, _ = pop(queue)
if p.T.Left != nil {
queue = push(queue, pair{p.T.Left, 2*idx + 1})
}
if p.T.Right != nil {
queue = push(queue, pair{p.T.Right, 2*idx + 2})
}
}
}
return res
}
// pushes a pair value to the queue
func push(queue []pair, val pair) []pair {
return append(queue, val)
}
// gets the first element and returns the updated queue
func pop(queue []pair) ([]pair, pair) {
return queue[1:], queue[0]
}
// returns the first and last element
func peek(queue []pair) (pair, pair) {
return queue[0], queue[len(queue)-1]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}