forked from youngyangyang04/leetcode-master
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1792c3
commit 293fba4
Showing
6 changed files
with
141 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
## 题目地址 | ||
|
||
## 思路 | ||
|
||
这道题目就是考察二叉树的层序遍历。 | ||
|
||
二叉树的层序遍历还有这两道题目,大家可以先做一下,其实都是一个思路 | ||
|
||
* [0102.二叉树的层序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0102.二叉树的层序遍历.md) 单纯的层寻遍历 | ||
* |[0107.二叉树的层次遍历II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0107.二叉树的层次遍历II.md) 相对于[0102.二叉树的层序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0102.二叉树的层序遍历.md)就是把每一层倒叙输出就可以了 | ||
|
||
想要学习二叉树的深度遍历可以看这里[彻底吃透前中后序递归法](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/solution/dai-ma-sui-xiang-lu-chi-tou-qian-zhong-hou-xu-de-d/)。 | ||
|
||
而本题呢,就是把每一层的结果求一个和,然后取平均值。 | ||
|
||
层序遍历一个二叉树,需要借用一个辅助数据结构队列来实现,**队列先进先出,符合一层一层遍历的逻辑,而是用栈先进后出适合模拟深度优先遍历也就是递归的逻辑。** | ||
|
||
使用队列实现广度优先遍历,动画如下: | ||
|
||
<video src='../video/102二叉树的层序遍历.mp4' controls='controls' width='640' height='320' autoplay='autoplay'> Your browser does not support the video tag.</video></div> | ||
|
||
这样就实现了层序从左到右遍历二叉树。 | ||
|
||
代码如下:(这份代码也可以作为二叉树层序遍历的模板)。 | ||
|
||
## C++代码 | ||
|
||
``` | ||
class Solution { | ||
public: | ||
vector<double> averageOfLevels(TreeNode* root) { | ||
queue<TreeNode*> que; | ||
if (root != NULL) que.push(root); | ||
vector<double> result; | ||
while (!que.empty()) { | ||
int size = que.size(); | ||
double sum = 0; // 统计每一层的和 | ||
for (int i = 0; i < size; i++) { // 这里一定要使用固定大小size,不要使用que.size() | ||
TreeNode* node = que.front(); | ||
que.pop(); | ||
sum += node->val; | ||
if (node->left) que.push(node->left); | ||
if (node->right) que.push(node->right); | ||
} | ||
result.push_back(sum / size); // 将每一层放进结果集 | ||
} | ||
return result; | ||
} | ||
}; | ||
``` |
Binary file not shown.