102. Binary Tree Level Order Traversal + 103. Binary Tree Zigzag Level Order Traversal + 107. Binary Tree Level Order Traversal II + 637. Average of Levels in Binary Tree
▶ 有关将一棵二叉树转化为二位表的题目,一模一样的套路出了四道题
▶ 第 102 题,简单的转化,[ 3, 9, 20, null, null, 15, 7 ] 转为 [ [ 15, 7 ] , [ 9, 20 ] , [ 3 ] ]
● 自己的代码,6 ms,先根序遍历,最快的解法算法与之相同
class Solution
{
public:
void visit(TreeNode* root, vector<vector<int>>& table, int level)
{
if (root == nullptr)
return;
if (table.size() <= level) // 首次到达该深度,建立新的一层
table.push_back(vector<int>());
visit(root->left, table, level + );
table[level].push_back(root->val);
visit(root->right, table, level + );
return;
}
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> output;
if (root == nullptr)
return output;
visit(root, output, );
return output;
}
};
▶ 第 103 题,要求奇数行顺序输出,偶数行倒序输出,在第 102 题的基础上加个函数 reverse() 就行
● 自己的代码,4 ms,基于第 102 题,最快的解法算法与之相同
class Solution
{
public:
void visit(TreeNode* root, vector<vector<int>>& table, int level)
{
if (root == nullptr)
return;
if (table.size() <= level) // 首次到达该深度,建立新的一层
table.push_back(vector<int>());
visit(root->left, table, level + );
table[level].push_back(root->val);
visit(root->right, table, level + );
return;
}
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> output;
if (root == nullptr)
return output;
visit(root, output, );
for (int i = ; i < output.size(); i += )
reverse(output[i].begin(), output[i].end());
return output;
}
};
▶ 第 107 题,要求倒序输出,还是在第 102 题 的基础上加个函数 reverse() 就行
● 自己的代码,5 ms,最快的解法算法与之相同,但是使用的数据结构是 list,新建一层用的是函数 resize()
class Solution
{
public:
void visit(TreeNode* root, vector<vector<int>>& table, int level)
{
if (root == nullptr)
return;
if (table.size() <= level) // 首次到达该深度,建立新的一层
table.push_back(vector<int>());
visit(root->left, table, level + );
table[level].push_back(root->val);
visit(root->right, table, level + );
return;
}
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> output;
if (root == nullptr)
return output;
visit(root, output, );
reverse(output.begin(), output.end());
return output;
}
};
▶ 第 637 题,在原来转化为二维表的基础上计算每一层的平均数,压成一维表输出
● 自己的代码,17 ms,基于第 102 题的遍历函数,输出二维表以后再一层一层计算平均数。最快的解法算法与之相同,但不再将树转化为表以后再算平均值,而是在遍历树的同时维护一个元素个数表和一个平均值表,每遍历一个非空结点就更新该层的元素个数和平均值
class Solution
{
public:
void visit(TreeNode* root, vector<vector<int>>& table, int level)
{
if (root == nullptr)
return;
if (table.size() <= level) // 首次到达该深度,建立新的一层
table.push_back(vector<int>());
visit(root->left, table, level + );
table[level].push_back(root->val);
visit(root->right, table, level + );
return;
}
vector<double> averageOfLevels(TreeNode* root)
{
vector<double> output;
if (root == nullptr)
return output;
int i, j;
double sum;
vector<vector<int>> table;
visit(root, table, );
for (i = ; i < table.size(); i++)
{
for (j = , sum = 0.0f; j < table[i].size(); j++)
sum += table[i][j];
output.push_back(sum / table[i].size());
}
return output;
}
};
102. Binary Tree Level Order Traversal + 103. Binary Tree Zigzag Level Order Traversal + 107. Binary Tree Level Order Traversal II + 637. Average of Levels in Binary Tree的更多相关文章
- 637. Average of Levels in Binary Tree - LeetCode
Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...
- 【Leetcode_easy】637. Average of Levels in Binary Tree
problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...
- [LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- 637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- LeetCode 637 Average of Levels in Binary Tree 解题报告
题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form ...
- LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)
题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o ...
- LeetCode - 637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- [LeetCode&Python] Problem 637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- 637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值
[抄题]: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...
随机推荐
- MVVM特点、源(数据)与目标(如:控件等)的映射
数据(源,viewMode表示)与控件(目标)的完全映射, 在绑定之后,通过操作数据,改变控件显示效果.显示内容.状态等.
- arcgis api for silverlight开发系列之二:缓存图层与动态图层及图层总结 .
本文摘自:http://blog.csdn.net/leesmn/article/details/6916458(很优秀的博客) 作为ESRI的平台的一份子arcgis api for silve ...
- 在小红家里面,有n组开关,触摸每个开关,可以使得一组灯泡点亮。
package april; import java.util.ArrayList; import java.util.Scanner; /** * * @ClassName: Class_9 * @ ...
- 尺取法拓展——POJ3320
#include <iostream> #include <cstdio> #include <algorithm> #include <set> #i ...
- linux下smb
smbclient用法 1,列出某个IP地址所提供的共享文件夹smbclient -L 198.168.0.1 -U username%password 2,像FTP客户端一样使用smbclients ...
- WEB标准以及W3C的理解和认识
web标准简单来说可以分为结构.表现和行为.其中结构主要是有HTML标签组成.表现即指css样式表,通过css可以是页面的结构标签更具美感.行为是指页面和用户具有一定的交互,同时页面结构或者表现发生变 ...
- hdu1512
题解: 每一次合并两个对 修改操作就和普通的堆一样 代码: #include<cstring> #include<cmath> #include<cstdio> # ...
- SQL 字符串拆分
字符串拆分: ALTER FUNCTION [dbo].[f_Split](@sText nvarchar(max),@split NVARCHAR(20)) RETURNS @t TABLE (id ...
- Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 04
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2287] 版本控制:https://git.coding.net/liuyy08 ...
- specialized English for automation-Lesson 1 Analog Amplifiers
要求每天阅读一篇技术文档,不需要记下来,只是能看懂就好..后发现,这就是专业英语的课程资料. ----------------------------------------------------- ...