-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.go
62 lines (51 loc) · 1.7 KB
/
Solution.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
package lc199
import (
"gitlab.com/euchangxian/leetcode/Go/internal/bst"
)
// RightSideView returns the values of the nodes as seen from the right side of the tree, ordered
// from top to bottom.
// Approach: Level by level, pick the right-most node => LevelOrderSearch.
func RightSideView(root *bst.TreeNode) []int {
var result []int
levelOrderSearch(&result, root)
return result
}
func levelOrderSearch(result *[]int, root *bst.TreeNode) {
if root == nil {
return
}
var frontier Queue // Slice-backed Queue
frontier.Enqueue(root) // Initialize the Queue; Add the root node
for !frontier.IsEmpty() {
var nextFrontier Queue
hasRightMost := false // Flag to add only the right-most node of each frontier to the results
for !frontier.IsEmpty() {
currentNode := frontier.Dequeue()
if !hasRightMost {
*result = append(*result, currentNode.Val)
hasRightMost = true
}
// Adds bst.TreeNode.Right, then bst.TreeNode.Left. In this way, the first node popped from
// each frontier will always be the right-most node, and will be in the result. *result = append(*result, currentNode.Val)
if currentNode.Right != nil {
nextFrontier = append(nextFrontier, currentNode.Right)
}
if currentNode.Left != nil {
nextFrontier = append(nextFrontier, currentNode.Left)
}
}
frontier = nextFrontier
}
}
type Queue []*bst.TreeNode
func (q *Queue) IsEmpty() bool {
return len(*q) == 0 // Assumes len() will never return negative.
}
func (q *Queue) Enqueue(node *bst.TreeNode) {
*q = append(*q, node)
}
func (q *Queue) Dequeue() *bst.TreeNode {
popped := (*q)[0] // Retrieves the first element in the Queue
*q = (*q)[1:] // Slices the Queue, removing the first element
return popped
}