LeetCode: Binary Tree Level Order Traversal && Binary Tree Zigzag Level Order Traversal
Title:
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]
] 思路:使用队列,并且每次记录下每层的数目,这样从队列中取元素也就清楚了每层的元素
/**
* 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) {
queue<TreeNode*> Q;
vector<vector<int> > results;
if (!root)
return results;
vector<int> result;
Q.push(root);
int count_pre = ;
int count_cur = ;
while (!Q.empty()){
TreeNode* top = Q.front();
result.push_back(top->val);
Q.pop();
count_pre--;
if (top->left){
Q.push(top->left);
count_cur++;
}
if (top->right){
Q.push(top->right);
count_cur++;
}
if (count_pre == ){
count_pre = count_cur;
count_cur = ;
results.push_back(result);
result.clear();
}
}
return results;
}
};
还可以使用递归。关键是要保存到对应的层数
/**
* 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) {
vector<vector<int> > result;
travel(root,result,);
return result;
}
void travel(TreeNode* root,vector<vector<int> > &result,int level){
if (!root)
return ;
if (level > result.size())
result.push_back(vector<int> ());
result[level-].push_back(root->val);
travel(root->left,result,level+);
travel(root->right,result,level+);
}
};
Title:
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
一样的思路啊!如果不使用递归,则使用一个标记,看这层是否反转,直接使用reverse函数即可。如果使用递归,则也是在相应的层反转。但是因为是在插入的过程中,所以应该是插入每层的vector头。
/**
* 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>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int> > results;
if (!root)
return results;
queue<TreeNode*> Q;
Q.push(root);
int count_pre = ;
int count_cur = ;
vector<int> result;
bool flag = false;
while (!Q.empty()){
TreeNode* node = Q.front();
Q.pop();
result.push_back(node->val);
count_pre--;
if (node->left){
Q.push(node->left);
count_cur++;
}
if (node->right){
Q.push(node->right);
count_cur++;
}
if (count_pre == ){
count_pre = count_cur;
count_cur = ;
if (flag){
reverse(result.begin(),result.end());
results.push_back(result);
}else{
results.push_back(result);
}
result.clear();
flag = !flag;
}
}
return results;
}
};
/**
* 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>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int> > results;
travel(root,,results,true);
return results;
}
void travel(TreeNode* root, int level, vector<vector<int> > &results, bool left_to_right){
if (!root)
return;
if (level > results.size())
results.push_back(vector<int>());
if (left_to_right)
results[level-].push_back(root->val);
else
results[level-].insert(results[level-].begin(),root->val);
travel(root->left,level+,results,!left_to_right);
travel(root->right,level+,results,!left_to_right);
}
};
LeetCode: Binary Tree Level Order Traversal && Binary Tree Zigzag Level Order Traversal的更多相关文章
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode]题解(python):103 Binary Tree Zigzag Level Order Traversal
题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, re ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- [leetcode]Binary Tree Zigzag Level Order Traversal @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tr ...
- LeetCode(103) Binary Tree Zigzag Level Order Traversal
题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...
- leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
- [LeetCode] 103. Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
- Binary Tree Zigzag Level Order Traversal [LeetCode]
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
随机推荐
- memcached+php客户端
连接memcached <?php $mem = new Memcache; $mem->connect('localhost',11211) or die("connected ...
- 翻译:用Javascript的Function构造器伪造上下文 by Ben Nadel
在我的jQuery模板标记语言(JTML)项目中,我需要一种方式将JTML模板编译到JS函数,这样它们就可以如期地在任何时候转换成新的HTML标记.但这是一个严峻的问题,因为JTML代码涉及非作用域( ...
- Unity3D研究院之打开Activity与调用JAVA代码传递参数
原地址:http://www.xuanyusong.com/archives/667 Unity for Android 比较特殊,Unity for IOS 打包是将XCODE工程直接交给开发 ...
- Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- hdu 1253 胜利大逃亡(广搜,队列,三维,简单)
题目 原来光搜是用队列的,深搜才用栈,我好白痴啊,居然搞错了 三维的基础的广搜题 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #in ...
- hdu 2112 HDU Today (最短路,字符处理)
题目 题目很简单,只是多了对地名转化为数字的处理,好吧,这我也是参考网上的处理办法,不过大多数的人采用map来处理 注意初始化注意范围,不然会wa!!!(这是我当时wa的原因org) 大家容易忽视的地 ...
- java基础知识回顾之---java String final类普通方法的应用之字符串数组排序
/* * 1,给定一个字符串数组.按照字典顺序进行从小到大的排序. * {"nba","abc","cba","zz", ...
- H5 移动Web框架集合
http://frozenui.github.io/ 一个简洁的h5前端框架 http://weui.github.io/weui/ 腾讯出的WebUI 风格是基于微信
- ibatis框架文件配置
最近2天在学ibatis,心里也有一些心得,就把它写下来了. 首先是配置一下ibatis的环境,添加ibatis2.X.jar,mysql-connection-bin.5.1.8.jar,建立一个w ...
- jsp是有自己的编译引擎的
什么是jsp? JSP(Java Server Pages)是指: 在HTML中嵌入Java脚本代码 由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本代码 然后将生成的整个页面信息返回给客户端 ...