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 ...
随机推荐
- 2018-2019-2 20165332 《网络对抗技术》Exp4 恶意代码分析
2018-2019-2 20165332 <网络对抗技术>Exp4 恶意代码分析 原理与实践说明 1.实践目标 监控你自己系统的运行状态,看有没有可疑的程序在运行. 分析一个恶意软件,就分 ...
- 如果从excel表中导出insert-sql
=CONCATENATE("INSERT INTO p_act_lottery(actId,status,grantWay,createTime,invalidTime,amount,pri ...
- Vue.js组件设计原则
页面上把每个独立可以交互的区域视为一个组件 每个组件对应一个工程目录,组件所需要的各种资源在这个目录下就近维护 页面不过是组件的容器,组件可以嵌套自由组合形成完整的页面
- UVALive 5903 Piece it together 二分匹配,拆点 难度:1
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- hdu5818
题解: 维护两个左偏树 按照左偏树模板来做 代码: #include<cstdio> #include<cmath> #include<algorithm> #in ...
- 利用javascript:void(0)制作假的提交按钮替代button
在写html页面,我们很自然的在表单提交的地方采用button来作为提交按钮,但是,用<button type=”button”>按钮</button>作为提交代码会有个问题, ...
- 二、深度解析HTML5之视频播放和音频播放
一:视频播放 传统的视频音频播放是通过flash插件的形式完成,不是所有的浏览器都安装了flash插件,而且手机端不支持flash,这就导致视频和音频的播放会有很大的麻烦. 于是,HTML5增加音频和 ...
- Mac 终端下Homebrew的几个常用命令(新手笔记)
最近在研究用appium来做IOS的自动化,所以开始接触Mac系统.记录一下在Mac的终端下Homebrew的几个常用命令 安装(需要 Ruby,不过一般自带都有):ruby -e "$(c ...
- html页面中event的常见应用
一:获取键盘上某个按键的unicode值 <html> <head> <script type="text/javascript"> funct ...
- Jmeter执行Java请求
Jmeter执行Java请求(QQ交流群:577439379) 一.打开Eclipse,创建一个Java工程 二.拷贝jmeter所依赖的jar包 将jmeter中,\lib\ext目录下的Apach ...