leetcode-102.层序遍历二叉树(正序)· BTree
题面
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
层序遍历二叉树,要求从上到下,从左到右,输出结果为二维vector。
样例
Given binary tree
[3,9,20,null,null,15,7],3
/ \
9 20
/ \
15 7return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
算法
想法:还记得怎么进行层序遍历吗?结果是一个一维数组。现在的情景是要把每一层用一个单独的vector存起来,仅此而已。那么我们的首要问题就是,怎么解决“一层”的问题。
其实,我们在处理完上一层时,queue中就会只包含有本层的节点指针,那么我们计算queue.size(),然后只处理queue的前queue.size()个元素不就可以了吗?在处理其中每一个元素的时候,判空。非空的话,值压入vector,然后把左孩子和有孩子都再次压入queue,最后queue.pop()。处理完这一层,就把当前vector整个压入结果vector<vector<int>> res 中。

1. 根节点判空,若空,返回空vector<vector<int>> (); 不空,继续
2. 新建queue<TreeNode*> q, 和结果vector<vector<int>> res,根节点压入q中
3. 若q非空,while循环判断
新建vector。计算q元素个数,即为一层节点,逐个判空,若不空,值压入vector。然后把子节点压入q中,然后把它弹出queue。
处理完这层,若vector不空,整个压入res中。
4. 返回 res 结束。
源码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
//层序遍历
if(root == nullptr)
return vector<vector<int>> ();
vector<vector<int>> res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
int size = q.size();
vector<int> tmp;
while(size--)
{
TreeNode* p = q.front();
if(p != nullptr)
{
tmp.push_back(p->val);
q.push(p->left);
q.push(p->right);
}
q.pop();
}
if(tmp.size() != )
res.push_back(tmp);
}
return res;
}
};
leetcode-102.层序遍历二叉树(正序)· BTree的更多相关文章
- 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出
用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...
- Python3 解析XML 层序遍历二叉树
Python3 解析XML 层序遍历二叉树 keyword : python3, xml, xml.dom.minidom, 层序遍历, 层次遍历, 二叉树 part1 问题描述 面对如下 XML 文 ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- 层序遍历二叉树 完整层序重建二叉树 python
给定一个二叉树的完整的层次遍历序列(包含所有节点,包括空节点),利用这个序列生成一颗二叉树. 我们首先来看怎样对一颗二叉树进行层序遍历,下图所示的二叉树层次遍历的结果为[a,b,c,d,e],在这个过 ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>
问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- PAT A1147 Heaps (30 分)——完全二叉树,层序遍历,后序遍历
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
随机推荐
- Qwidget::update
void QWidget::update ()分析重绘事件激活 1看看手册中这段话 void QWidget::update () [slot] Updates the widget unless u ...
- PAT 甲级 1056 Mice and Rice (25 分) (队列,读不懂题,读懂了一遍过)
1056 Mice and Rice (25 分) Mice and Rice is the name of a programming contest in which each program ...
- LeetCode_88. Merge Sorted Array
88. Merge Sorted Array Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- iOS中处理json解析出现的null,nil的解决办法
最开始是使用的一个函数进行处理,代码如下: - (id) setNoNull:(id)aValue{ if (aValue == nil) { aValue = @"";//为nu ...
- ADFS2016和ADFS代理的安装与配置
一,ADFS安装教程 教程链接(包含安装和配置两个步骤): https://www.virtuallyboring.com/how-to-setup-microsoft-active-director ...
- Python之汉诺塔递归运算
汉诺塔问题是一个经典的问题.汉诺塔(Hanoi Tower),又称河内塔,源于印度一个古老传说.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆 ...
- iOS-UILabel的使用
常用属性UILabel //显示的文字 @property(nonatomic,copy) NSString *text; //字体 @property(nonatomic, ...
- html转图片网页截屏(三),puppeteer
puppeteer谷歌出品,是一个 Node 库,它提供了一个高级 API 来通过 DevTools 协议控制 Chromium 或 Chrome. 官方github地址:https://github ...
- OpenCV计算机视觉实战(Python版)资源
疲劳检测 pan.baidu.com/s/1Ng_-utB8BSrXlgVelc8ovw #导入工具包 from scipy.spatial import distance as dist from ...
- Andrew Ng机器学习课程13
Andrew Ng机器学习课程13 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 引言:主要从一般的角度介绍EM算法及其思想,并推导了EM算法的收敛性.最后 ...