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 4786b6e

Browse files
add LeetCode 199. 二叉树的右视图
1 parent e7d8936 commit 4786b6e

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
6+
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
7+
8+
示例:
9+
10+
```javascript
11+
输入: [1,2,3,null,5,null,4]
12+
输出: [1, 3, 4]
13+
解释:
14+
15+
1 <---
16+
/ \
17+
2 3 <---
18+
\ \
19+
5 4 <---
20+
```
21+
22+
来源:力扣(LeetCode)
23+
链接:https://leetcode-cn.com/problems/binary-tree-right-side-view
24+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
25+
26+
27+
## 解题思路
28+
`DFS`,每一层只能取一个元素,那么我们搜的时候优先考虑右孩子即可。
29+
![](https://img-blog.csdnimg.cn/20200925163146472.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
30+
参考 <a href="https://leetcode-cn.com/problems/binary-tree-right-side-view/solution/shen-du-you-xian-sou-suo-by-shetia-2/">shetia</a> 大佬图解
31+
32+
```javascript
33+
/**
34+
* Definition for a binary tree node.
35+
* function TreeNode(val) {
36+
* this.val = val;
37+
* this.left = this.right = null;
38+
* }
39+
*/
40+
/**
41+
* @param {TreeNode} root
42+
* @return {number[]}
43+
*/
44+
var rightSideView = function(root) {
45+
if(!root) return [];
46+
let res = [];
47+
let dfs = (step,root) => {
48+
if(res.length === step){
49+
res.push(root.val);
50+
}
51+
// 优先遍历右孩子,再遍历左孩子
52+
root.right && dfs(step+1,root.right);
53+
root.left && dfs(step+1,root.left);
54+
}
55+
dfs(0,root);
56+
return res;
57+
};
58+
```
59+
60+
**解法二**
61+
62+
`BFS`,对于每一层取队列中对首元素,然后放入队列的时候,如果有对应左右孩子的话,优先放右孩子,再放左孩子。
63+
64+
```javascript
65+
/**
66+
* Definition for a binary tree node.
67+
* function TreeNode(val) {
68+
* this.val = val;
69+
* this.left = this.right = null;
70+
* }
71+
*/
72+
/**
73+
* @param {TreeNode} root
74+
* @return {number[]}
75+
*/
76+
var rightSideView = function (root) {
77+
if (!root) return [];
78+
let queue = [root];
79+
let res = [];
80+
while (queue.length) {
81+
let size = queue.length;
82+
res.push(queue[0].val);
83+
while (size--) {
84+
let node = queue.shift();
85+
// 优先放右孩子
86+
node.right && queue.push(node.right);
87+
node.left && queue.push(node.left);
88+
}
89+
}
90+
return res;
91+
};
92+
```
93+
94+
## 最后
95+
文章产出不易,还望各位小伙伴们支持一波!
96+
97+
往期精选:
98+
99+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
100+
101+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
102+
103+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
104+
105+
106+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
107+
108+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
109+
110+
```javascript
111+
学如逆水行舟,不进则退
112+
```
113+
114+
115+

0 commit comments

Comments
(0)

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