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 返回其 ...
随机推荐
- iOS launchImage
iOS launchImage https://stackoverflow.com/questions/34027270/ios-launch-screen-in-react-native 如何设置: ...
- 国内写的比较好的markdown教程
国内写的比较好的markdown教程,预览图如下 点击跳转
- git代理配置
命令行模式下配置 git config --global https.proxy https://proxyuser:proxypassword@ip/域名:port git config --glo ...
- linux下tomcat启动慢解决方法
前言 最近在工作中遇到一个问题,在Linux下Tomcat 8启动很慢,且日志上无任何错误,在日志中查看到如下信息: Log4j:[2017-08-2715:47:11] INFO ReadPrope ...
- 【python】python打包生成的exe文件运行时提示缺少模块
事情是这样的我用打包命令:pyinstaller -F E:\python\clpicdownload\mypython.py打包了一个exe程序,但是运行时提示我缺 少bs4模块然后我就去查pyin ...
- 斐讯N1折腾记
斐讯N1折腾记:运行 Linux 及优化 2018-06-23 37条评论 4,445次阅读 11人点赞 最后更新时间:2019年03月10日 咳咳咳,上篇教程教大家给斐讯 N1 降级并且刷了 ...
- 黑电平校正BLC
参考:https://www.cnblogs.com/zhangAlin/p/10661763.html
- Win10系统下Anaconda下安装多种Python函数库
建议直接安装Anaconda,这是一个包含Numpy,Pandas,Sklearn等函数库的计算机科学软件包,下面的软件可以在此环境下进行安装下载. 一.计算机视觉 1. OpenCV图像处理 在ht ...
- python 函数内使用自己的函数名
def p(): import sys print sys._getframe(1).f_code.co_name def f(): p() def f1(): p() if __name__ == ...
- CSS Core Technology
1. Selector Different types of selectors: Selectors can be divided into the following categories: Si ...