Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5c845b

Browse files
committed
commit solution 199
1 parent 6e11fe0 commit f5c845b

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [199. 二叉树的右视图](https://leetcode-cn.com/problems/binary-tree-right-side-view/description/)
2+
3+
### 题目描述
4+
5+
<p>给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。</p>
6+
7+
<p><strong>示例:</strong></p>
8+
9+
<pre><strong>输入:</strong>&nbsp;[1,2,3,null,5,null,4]
10+
<strong>输出:</strong>&nbsp;[1, 3, 4]
11+
<strong>解释:
12+
</strong>
13+
1 &lt;---
14+
/ \
15+
2 3 &lt;---
16+
\ \
17+
5 4 &lt;---
18+
</pre>
19+
20+
### 解题思路
21+
22+
1. BFS
23+
24+
### 具体解法
25+
26+
27+
#### **Golang**
28+
```go
29+
type TreeNode struct {
30+
Val int
31+
Left *TreeNode
32+
Right *TreeNode
33+
}
34+
35+
func rightSideView(root *TreeNode) []int {
36+
res := []int{}
37+
if root == nil {
38+
return res
39+
}
40+
queue := []*TreeNode{root}
41+
42+
res = []int{root.Val}
43+
for len(queue) > 0 {
44+
l := len(queue)
45+
flag := 0
46+
for i := 0; i < l; i++ {
47+
node := queue[i]
48+
if node.Right != nil {
49+
if flag == 0 {
50+
flag = node.Right.Val
51+
}
52+
queue = append(queue, node.Right)
53+
}
54+
if node.Left != nil {
55+
if flag == 0 {
56+
flag = node.Left.Val
57+
}
58+
queue = append(queue, node.Left)
59+
}
60+
}
61+
if flag != 0 {
62+
res = append(res, flag)
63+
}
64+
queue = queue[l:]
65+
}
66+
return res
67+
}
68+
```
69+
70+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=199 lang=golang
5+
*
6+
* [199] 二叉树的右视图
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
type TreeNode struct {
19+
Val int
20+
Left *TreeNode
21+
Right *TreeNode
22+
}
23+
24+
func rightSideView(root *TreeNode) []int {
25+
res := []int{}
26+
if root == nil {
27+
return res
28+
}
29+
queue := []*TreeNode{root}
30+
31+
res = []int{root.Val}
32+
for len(queue) > 0 {
33+
l := len(queue)
34+
flag := 0
35+
for i := 0; i < l; i++ {
36+
node := queue[i]
37+
if node.Right != nil {
38+
if flag == 0 {
39+
flag = node.Right.Val
40+
}
41+
queue = append(queue, node.Right)
42+
}
43+
if node.Left != nil {
44+
if flag == 0 {
45+
flag = node.Left.Val
46+
}
47+
queue = append(queue, node.Left)
48+
}
49+
}
50+
if flag != 0 {
51+
res = append(res, flag)
52+
}
53+
queue = queue[l:]
54+
}
55+
return res
56+
}
57+
58+
// @lc code=end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestRightSideView(t *testing.T) {
8+
root := &TreeNode{
9+
Val: 1,
10+
Left: &TreeNode{
11+
Val: 2,
12+
Left: nil,
13+
Right: nil,
14+
},
15+
Right: nil,
16+
}
17+
ret := []int{1, 2}
18+
nums := rightSideView(root)
19+
for i, v := range nums {
20+
if ret[i] != v {
21+
t.Fatalf("case fails %v\n", ret)
22+
}
23+
}
24+
25+
root1 := &TreeNode{}
26+
nums1 := rightSideView(root1)
27+
if len(nums1) > 1 {
28+
t.Fatalf("case fails %v\n", nums1)
29+
}
30+
31+
root2 := &TreeNode{
32+
Val: 1,
33+
Left: &TreeNode{
34+
Val: 2,
35+
Left: nil,
36+
Right: nil,
37+
},
38+
Right: &TreeNode{
39+
Val: 3,
40+
Left: &TreeNode{
41+
Val: 5,
42+
Left: nil,
43+
Right: nil,
44+
},
45+
Right: &TreeNode{
46+
Val: 6,
47+
Left: nil,
48+
Right: nil,
49+
},
50+
},
51+
}
52+
ret2 := []int{1, 3, 6}
53+
nums2 := rightSideView(root2)
54+
for i, v := range nums2 {
55+
if ret2[i] != v {
56+
t.Fatalf("case fails %v\n", nums2)
57+
}
58+
}
59+
60+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /