LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:Given binary tree [3,9,20,null,null,15,7]
/ \
/ \
return its level order traversal as:
[
[],
[,],
[,]
]
二叉树的层次遍历使用队列来实现,很大程度上类似于求树的最大深度。
第一种解法:开始时当前结点数curLevelCount=1,nextLevelCount=0,根据curLevelCount来输入每一层的结点值,当其左右子树存在时,将左右子树存入队列,并nextLevelCount++,每弹出一个当前层的结点时,curLevelCount--,当其为0时,再创建一个新的vector。
C++:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res={};
queue<TreeNode*> q;
if(!root)
return res;
q.push(root);
int curLevelCount=,nextLevelCount=;
vector<int> temp={};
while(!q.empty()){
TreeNode* cur=q.front();
temp.push_back(cur->val);
q.pop();
curLevelCount--;
if(cur->left){
q.push(cur->left);
nextLevelCount++;
}
if(cur->right){
q.push(cur->right);
nextLevelCount++;
}
if(curLevelCount==){
res.push_back(temp);
temp={};
curLevelCount=nextLevelCount;
nextLevelCount=;
}
}
return res;
}
第二种方法不用参数计算每一层结点个数,利用for循环,每一层时利用队列的大小来计算每一层的结点数。两层循环嵌套,效率比较低。
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res={};
queue<TreeNode*> q;
if(!root)
return res;
q.push(root);
while(!q.empty()){
vector<int> temp={};
for(int i=q.size();i>;i--){
TreeNode* cur=q.front();
temp.push_back(cur->val);
q.pop();
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
}
res.push_back(temp);
}
return res;
}
LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++的更多相关文章
- leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历
基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...
- LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)
这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...
- 102 Binary Tree Level Order Traversal 二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 ...
- [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...
- Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS
二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...
随机推荐
- 对于Vue的v-if 和 v-showi
V-if : 1. 是条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当的被销毁和重建. 2. 是惰性的,如果初始渲染条件为假,则什么都不做,直到条件第一次变为真的时候,开开始渲染 ...
- 《用Python玩转数据》项目—线性回归分析入门之波士顿房价预测(二)
接上一部分,此篇将用tensorflow建立神经网络,对波士顿房价数据进行简单建模预测. 二.使用tensorflow拟合boston房价datasets 1.数据处理依然利用sklearn来分训练集 ...
- java虚拟机 之 垃圾回收机制
一.如何判断对象已死 垃圾回收器并不是java独有的,垃圾回收器的作用就是回收对象释放内存空间,那么如何判断哪些对象应该被回收呢? 在Java语言中是采用GC Roots来解决这个问题.如果一个对象和 ...
- 20175202 《Java程序设计》第三周学习总结
20175209 2018-2019-2 <Java程序设计>第三周学习总结 教材知识点总结 1.编程语言发展阶段: 面向机器语言——面向过程语言——面向对象语言. 2.类声明: 类名必须 ...
- gcc 常用指令
gcc hello.c 直接编译默认输出 a.out可执行文件 gcc hello.c -o hello 生成名字为hello的可执行文件 gcc hello.c -o hello -Wa ...
- Application、QueryString、session、cookie、ViewState、Server.Transfer等
Application: WebForm1.aspx: protected void Button1_Click(object sender, EventArgs e) { ; Response.Re ...
- 导入tensorflow时DLL load failed: 找不到指定的模块
简单暴力:卸载 重装 方法一: 先删除:pip uninstall tensorflow 再下载:pip install tensorflow 方法二: 也有可能是numpy版本不匹配的问题: 卸载: ...
- ID 生成器 雪花算法
https://blog.csdn.net/wangming520liwei/article/details/80843248 ID 生成器 雪花算法 2018年06月28日 14:58:43 wan ...
- VSCode扩展包离线安装
下载离线包 下载地址:https://marketplace.visualstudio.com/vscode 安装离线包
- mysql5.7基于gtid的主从复制
两个节点:mkdir -p /data/mysql-5721/datamkdir -p /data/mysql-5721/innodbmkdir -p /data/mysql-5721/replica ...