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 819be30

Browse files
🐱(tree): 107. 二叉树的层序遍历 II
1 parent 37cc227 commit 819be30

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎docs/data-structure/tree/bfs/README.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,41 @@ func levelOrder(root *TreeNode) [][]int {
8080
}
8181
```
8282

83+
## 107. 二叉树的层序遍历 II
84+
85+
[原题链接](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/)
86+
87+
与 106 类似,只是结果输出顺序不同。
88+
89+
```python
90+
# Definition for a binary tree node.
91+
# class TreeNode:
92+
# def __init__(self, x):
93+
# self.val = x
94+
# self.left = None
95+
# self.right = None
96+
97+
class Solution:
98+
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
99+
ans = []
100+
q = [root]
101+
while len(q) != 0:
102+
# 遍历一层
103+
tmp = []
104+
for i in range(len(q)):
105+
top = q[0]
106+
del q[0]
107+
if top is None:
108+
continue
109+
tmp.append(top.val)
110+
q.append(top.left)
111+
q.append(top.right)
112+
if len(tmp) != 0:
113+
ans.append(tmp)
114+
115+
return ans[::-1]
116+
```
117+
83118
<!-- tabs:end -->
84119

85120
## 108. 将有序数组转换为二叉搜索树

0 commit comments

Comments
(0)

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