利用二叉树层序遍历输出每层数据
2017-08-31 21:29
393 查看
107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree
return its bottom-up level order traversal as:
本题的思路也是层序遍历,同时通过控制队列大小来控制位于同一层,可以认为与求二叉树每层平均值思路一致。
代码如下:
/**
* 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>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> v1;
vector<vector<int>> v;
vector<int> v2;
TreeNode *temp = root;
queue<TreeNode *> q;
if (temp == NULL)
return v;
q.push(temp);
v2.push_back(temp->val);
v1.push_back(v2);
v2.clear();
while (!q.empty())
{
int i = q.size();
for (int j = 0; j < i; j++)
{
TreeNode *top = q.front();
q.pop();
if (top->left != NULL)
{
q.push(top->left);
v2.push_back(top->left->
4000
val);
}
if (top->right != NULL)
{
q.push(top->right);
v2.push_back(top->right->val);
}
}
if (!v2.empty())
v1.push_back(v2);
v2.clear();
}
while (!v1.empty())
{
v.push_back(v1.back());
v1.pop_back();
}
return v;
}
};
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree
[3,9,20,null,null,15,7],
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
本题的思路也是层序遍历,同时通过控制队列大小来控制位于同一层,可以认为与求二叉树每层平均值思路一致。
代码如下:
/**
* 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>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> v1;
vector<vector<int>> v;
vector<int> v2;
TreeNode *temp = root;
queue<TreeNode *> q;
if (temp == NULL)
return v;
q.push(temp);
v2.push_back(temp->val);
v1.push_back(v2);
v2.clear();
while (!q.empty())
{
int i = q.size();
for (int j = 0; j < i; j++)
{
TreeNode *top = q.front();
q.pop();
if (top->left != NULL)
{
q.push(top->left);
v2.push_back(top->left->
4000
val);
}
if (top->right != NULL)
{
q.push(top->right);
v2.push_back(top->right->val);
}
}
if (!v2.empty())
v1.push_back(v2);
v2.clear();
}
while (!v1.empty())
{
v.push_back(v1.back());
v1.pop_back();
}
return v;
}
};
相关文章推荐
- 利用先序和中序非递归恢复二叉树,并后序遍历输出
- 二叉树:利用两个队列层次遍历输出指定层数的叶子节点
- 【数据结构与算法】二叉树的层序遍历
- 括号法递归建立二叉树并层序遍历输出
- 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。
- 数据结构_输出二叉树中先序、中序、后序遍历方式中第k个节点的数据
- 数据结构——树(3):二叉树的前中后层序遍历,Morris遍历比较试验
- 【数据结构与算法】二叉树的层序遍历
- C语言 数据结构之二叉树 数据结构实验之二叉树五:层序遍历
- 【数据结构与算法】二叉树的层序遍历
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
- 【数据结构与算法】二叉树的层序遍历
- 数据结构之二叉树的前序遍历、中序遍历、后序遍历、层序遍历
- 【数据结构与算法】二叉树的遍历(递归遍历、非递归遍历、层序遍历)
- 数据结构 利用循环队列层次遍历一棵二叉树 递归实现
- 【数据结构作业四】以二叉链表作存储结构,建立一棵二叉树,并输出该二叉树的先序、中序、后序遍历序列、高度和其叶子结点数。
- 数据结构之二叉树的前序遍历、中序遍历、后序遍历、层序遍历
- 建一棵二叉树,能够输入树的各个结点,并能够输出用不同方法遍历的遍历序列;分别建立二叉树存储结构的的输入函数、输出层序遍历序列的函数、输出先序遍历序列的函数
- 数据结构作业:利用中序遍历和后序遍历构建二叉树(RMQ转LCA)
- LeetCode二叉树的层序遍历的输出