(二叉树 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 right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
---------------------------------------------------------------------------------------------------------------------
这个是在leetcode102. Binary Tree Level Order Traversal的基础上拓展来的,只要加一个判断就行。
C++代码:
/**
* 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) {
if(!root) return {};
queue<TreeNode*> q;
q.push(root);
vector<vector<int> > vec;
bool flag = true;
while(!q.empty()){
vector<int> v;
for(int i = q.size(); i > ; i--){
auto t = q.front();
q.pop();
v.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
if(!flag)
reverse(v.begin(),v.end());
flag = !flag;
vec.push_back(v);
}
return vec;
}
};
(二叉树 BFS) leetcode103. Binary Tree Zigzag Level Order Traversal的更多相关文章
- 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 ...
- Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- 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解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...
- 【leetcode】Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
随机推荐
- CentOS7 搭建影梭服务器
安装Python包管理工具 yum install python-setuptools && easy_install pip 安装Shadowsocks pip install sh ...
- 安装mysql zip5.7版--安裝
一直以来都习惯了使用MySQL安装文件(.exe),今天下载了一个.zip版本的MySQL,安装过程中遇到了一些问题,如下: 1.在MySQL官网上(http://dev.mysql.com/down ...
- Linux 学习 (一) Linux简介
Linux达人养成计划 I 学习笔记 Linux 内核官网:www.kernel.org 内核版本说明:主版本.次版本.末版本,如2.6.18 Linux 主要发行版本 RedHat: 服务器领域,部 ...
- 12.k8s的存储卷创建过程
数据持久化需要数据卷.kubernetes生态提供海量的存储驱动和存储使用方式. [root@master song]# cat pod-demo.yml apiVersion: v1 kind: P ...
- 洛谷P3389 【模板】高斯消元法
P3389 [模板]高斯消元法 题目背景 Gauss消元 题目描述 给定一个线性方程组,对其求解 输入输出格式 输入格式: 第一行,一个正整数 n 第二至 n+1行,每行 n+1 个整数,为a1,a ...
- 重写Distinct
添加类并继承`IEqualityComparer`,重写方法 public class DistinctComparer : IEqualityComparer<ActionInfo> { ...
- 日期T转换
日期转换 在startup.csd ConfigureServices方法里 services.AddMvc().AddJsonOptions(o => { o.SerializerSettin ...
- C# MD5,hmacSHA1
一 MD5 推荐使用: md5 MD5 md5Hasher = MD5.Create(); byte[] data = md5Hasher.ComputeHash(Encoding.Default.G ...
- Python中pandas模块解析
Pandas基于两种数据类型: series 与 dataframe . 1.Series 一个series是一个一维的数据类型,其中每一个元素都有一个标签.类似于Numpy中元素带标签的数组.其中, ...
- Codeforces997D Cycles in product 【FFT】【树形DP】
题目大意: 给两个树,求环的个数. 题目分析: 出题人摆错题号系列. 通过画图很容易就能想到把新图拆在两个树上,在树上游走成环. 考虑DP状态F,G,T.F表示最终答案,T表示儿子不考虑父亲,G表示父 ...