Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 [3,9,20,null,null,15,7],
3 / \ 9 20 / \ 15 7
返回锯齿形层次遍历如下:
[ [3], [20,9], [15,7] ]
class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode* root)
{
vector<vector<int> > res;
if(root == NULL)
return res;
queue<TreeNode*> q;
q.push(root);
int cnt = 0;
while(!q.empty())
{
int size = q.size();
vector<int> temp;
for(int i = 0; i < size; i++)
{
TreeNode *node = q.front();
q.pop();
temp.push_back(node ->val);
if(node ->left)
q.push(node ->left);
if(node ->right)
q.push(node ->right);
}
if((cnt & 1) == 1)
{
reverse(temp.begin(), temp.end());
}
res.push_back(temp);
cnt++;
}
return res;
}
};
Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历的更多相关文章
- 103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行).例如:给定二叉树 [3,9,20,null,null,15,7], 3 ...
- [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 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 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- (二叉树 BFS) leetcode103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 32-3题:LeetCode103. Binary Tree Zigzag Level Order Traversal锯齿形层次遍历/之字形打印二叉树
题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...
- [Leetcode] Binary tree Zigzag level order traversal二叉树Z形层次遍历
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二叉树来回遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- LeetCode103 Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
随机推荐
- vuex的使用入门-官方用例
store/index.js import Vue from 'vue' import Vuex from 'vuex'; // 使用vuex Vue.use(Vuex) const store = ...
- (转)Lua语言实现简单的多线程模型
转自: https://blog.csdn.net/john_crash/article/details/49489609 lua本身是不支持真正的多线程的,但是lua提供了相应的机制来实现多线程.l ...
- axios——post请求时把对象obj数据转为formdata格式
转载自:https://blog.csdn.net/feizhong_web/article/details/80514436 在调用后台接口的时候,上传报名信息,利用axios 的post请求,发 ...
- jdk11.0.2安装
1.去官网下载合适的jdk 网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.ht ...
- WPF 深入浅出学习 Day1
- hdu 5382
\(F(n)=\sum_{i=1}^n\sum_{j=1}^n[lcm(i,j)+gcd(i,j)\geq n]\) \(S(n)=\sum_{i=1}^nF(i)\) \(F(n)=n^2-\sum ...
- PHP接收数据数据包的几个方式
PHP默认识别的数据类型是application/x-www.form-urlencoded标准的数据类型. php获取post参数的几种方式 1.$_POST['paramName'] 只能接收Co ...
- Entity Framework Extended 批量删除
public static class DbContextExtensions { public static void DeleteBatch<T>(this DbContext con ...
- JDK源码阅读--ArrayList
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess ...
- JDK源码阅读--Object
在java.lang包下 Object类:是所有类的基类(父类) public final native Class<?> getClass(); 返回这个Object所代表的的运行时类 ...