Skip to content

Commit 17dedb1

Browse files
authored
Update 199._binary_tree_right_side_view.md
1 parent ea5954e commit 17dedb1

File tree

1 file changed

+38
-50
lines changed

1 file changed

+38
-50
lines changed
Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,62 @@
1-
###199. Binary Tree Right Side View
1+
# 199. Binary Tree Right Side View
22

3-
题目:
3+
**<font color=red>难度: Medium</font>**
44

5-
<https://leetcode.com/problems/binary-tree-right-side-view/>
5+
## 刷题内容
66

7+
> 原题连接
78
8-
难度:
9+
* https://leetcode.com/problems/binary-tree-right-side-view/description/
910

10-
Medium
11+
> 内容描述
1112
13+
```
1214
13-
还是在玩第102题,level order traversal.
15+
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
1416
15-
```
16-
class Solution(object):
17-
def rightSideView(self, root):
18-
"""
19-
:type root: TreeNode
20-
:rtype: List[int]
21-
"""
22-
if root == None: return []
23-
24-
res = []
25-
curLevel = [root]
26-
while curLevel:
27-
nextLevel = []
28-
tmpRes = []
29-
for node in curLevel:
30-
tmpRes.append(node.val)
31-
if node.left: nextLevel.append(node.left)
32-
if node.right: nextLevel.append(node.right)
33-
res.append(tmpRes[-1])
34-
curLevel = nextLevel
35-
return res
36-
```
17+
Example:
3718
19+
Input: [1,2,3,null,5,null,4]
20+
Output: [1, 3, 4]
21+
Explanation:
3822
39-
另一个解法
23+
1 <---
24+
/ \
25+
2 3 <---
26+
\ \
27+
5 4 <---
28+
```
4029

41-
**解题思路**
30+
## 解题方案
4231

43-
这个题目还是挺有意思的,它还可以问左视图。解法上,直观些,取得每一层的最右边一个元素即可。既然取每一层最右边元素,那就要对二叉树进行层次遍历,层次遍历比较简单,就是使用一个队列,进队根节点,然后循环,出队列一个元素,左子树不空就进队,右子树不空就进队,直到队列为空时停止循环。但要想取得每层的最右一个元素,难点是**如何知道当前节点是哪一层**
32+
> 思路 1
33+
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******
4434

45-
对于一棵树,除了根节点,其他节点我们都不能直接知道它是哪一层,但是根节点我们肯定知道是第一层。那根节点的左右孩子节点是第几次?推广一下,如果我们知道节点 `x` 是第 `k` 层,那节点 `x` 的左右子节点是第几层?回答出这个问题,难点就迎刃而解了。
4635

47-
我们在层次遍历的队列里为每个节点增加一个字段表示深度(即该节点在第几层),然后在其子节点入队列的时候,设置子节点的深度为父节点的深度加一。因为我们知道根节点的深度,所以可以很容易的递推下去。
36+
还是在玩第102题,level order traversal. beats 100%
4837

4938
```python
50-
class Solution:
39+
class Solution(object):
5140
def rightSideView(self, root):
5241
"""
5342
:type root: TreeNode
5443
:rtype: List[int]
5544
"""
56-
if not root: # 特殊情况检查
45+
if not root:
5746
return []
58-
queue, seq = [(1, root)], [] # queue为层次遍历的操作队列,seq保存遍历的结果
59-
while queue:
60-
deep, node = queue.pop(0) # queue 内的元素是个(deep, node)二元组
61-
seq.append((deep, node.val)) # seq 内的元素是个(deep, node.val)二元组
62-
if node.left:
63-
queue.append((deep+1, node.left))
64-
if node.right:
65-
queue.append((deep+1, node.right))
66-
last, out = -1, [] # 因为要同层的最右边一个,也就是seq中,deep相同的几个里面最后一个
67-
for deep, val in reversed(seq): # 所以将seq反转,deep相同的第一个即最右一个
68-
if deep != last: # last 表示前一层的deep值
69-
last = deep
70-
out.append(val)
71-
return list(reversed(out)) # 最后把结果反转回来
47+
48+
res = []
49+
cur_level = [root]
50+
while cur_level:
51+
next_level, tmp_res = [], []
52+
for node in cur_level:
53+
tmp_res.append(node.val)
54+
if node.left:
55+
next_level.append(node.left)
56+
if node.right:
57+
next_level.append(node.right)
58+
res.append(tmp_res[-1])
59+
cur_level = next_level
60+
return res
7261
```
7362

74-
用时 52ms,战胜 86% python3 提交。

0 commit comments

Comments
 (0)