Skip to content

Commit 2e73bbd

Browse files
committed
binary_tree_right_side_view
1 parent 66024c3 commit 2e73bbd

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
157157
#### [190. Reverse Bits](https://github.com/hitzzc/go-leetcode/tree/master/reverse_bits)
158158
#### [191. Number of 1 Bits](https://github.com/hitzzc/go-leetcode/tree/master/number_of_1bits)
159159
#### [198. House Robber](https://github.com/hitzzc/go-leetcode/tree/master/house_robber)
160+
#### [199. Binary Tree Right Side View](https://github.com/hitzzc/go-leetcode/tree/master/binary_tree_right_side_view)
160161

161162

162163

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package binary_tree_right_side_view
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func rightSideView(root *TreeNode) []int {
10+
if root == nil {
11+
return []int{}
12+
}
13+
rets := []int{}
14+
queue := []*TreeNode{root}
15+
last := root
16+
for len(queue) != 0 {
17+
top := queue[0]
18+
queue = queue[1:]
19+
if top.Left != nil {
20+
queue = append(queue, top.Left)
21+
}
22+
if top.Right != nil {
23+
queue = append(queue, top.Right)
24+
}
25+
if top == last {
26+
rets = append(rets, top.Val)
27+
if len(queue) != 0 {
28+
last = queue[len(queue)-1]
29+
}
30+
}
31+
}
32+
return rets
33+
}

0 commit comments

Comments
 (0)