LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal
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,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
] 本文地址
队列辅助层序遍历,队列中插入NULL作为层与层之间的间隔,注意处理队列里最后的NULL时,不能再把它入队列以免形成死循环
/**
* Definition for binary tree
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<int> >res;
if(root == NULL)return res;
queue<TreeNode*> myqueue;
myqueue.push(root);
myqueue.push(NULL);//NULL是层与层之间间隔标志
vector<int> level;
while(myqueue.empty() == false)
{
TreeNode *p = myqueue.front();
myqueue.pop();
if(p != NULL)
{
level.push_back(p->val);
if(p->left)myqueue.push(p->left);
if(p->right)myqueue.push(p->right);
}
else
{
res.push_back(level);
if(myqueue.empty() == false)
{
level.clear();
myqueue.push(NULL);
}
}
}
return res;
}
};
LeetCode: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 {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7]
[9,20],
[3],
]
和上一题差不多,只是需要把最后遍历结果数组翻转一下
/**
* Definition for binary tree
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<int> >res;
if(root == NULL)return res;
queue<TreeNode*> myqueue;
myqueue.push(root);
myqueue.push(NULL);//NULL是层与层之间间隔标志
vector<int> level;
while(myqueue.empty() == false)
{
TreeNode *p = myqueue.front();
myqueue.pop();
if(p != NULL)
{
level.push_back(p->val);
if(p->left)myqueue.push(p->left);
if(p->right)myqueue.push(p->right);
}
else
{
res.push_back(level);
if(myqueue.empty() == false)
{
level.clear();
myqueue.push(NULL);
}
}
}
reverse(res.begin(), res.end());
return res;
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440542.html
LeetCode:Binary Tree Level Order Traversal I II的更多相关文章
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [leetcode]Binary Tree Level Order Traversal II @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...
- [Leetcode] Binary tree level order traversal ii二叉树层次遍历
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode: Binary Tree Level Order Traversal 解题报告
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- LeetCode——Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode - Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
随机推荐
- 《慕客网:IOS-动画入门》学习笔记
新建Cocoa Touch Class,语言是swift 然后继续为界面添加一个普通的View Controller,并且添加前面视图的静态table的转向剪头指向这个View Controller, ...
- UVa 106 - Fermat vs Pythagoras(数论题目)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 利用 cos 组件实现jsp中上传附件
需求:在web功能中附件上传功能为最基本的功能之一,所以用cos组件做了一个附件上传的demo.附件上传功能的实现可以利用其它的java组件实现,相关资料网上比较多. 说明步骤:下载组件并安装 --& ...
- 安装node.js+express for win7的Web开发环境配置
1.安装 node.js. 进入官网的下载地址:http://www.nodejs.org/download/ . 选择Windows Installer或者选择Windows Installer ( ...
- Busybox下mdev配置说明
[TOC] mdev说明 mdev是busybox自带的一个简化版的udev,适合嵌入式应用场合.其具有使用简单的特点.它的作用就是在系统启动和热插拔或动态加载驱动程序时,自动产生驱动程序所需要的节点 ...
- hdu 2874 Connections between cities [LCA] (lca->rmq)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- apache加载php配置
#载入php模块和ini路径,以及凡是.php开头的以它来处理 LoadModule php5_module E:/server/php/php5apache2_2.dll PHPIniDir &qu ...
- Webpack+React+ES6入门指南[转]
React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React,组件化似乎不再步履蹒跚,有了Reac ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
- [转载]ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件
作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...