Binary Tree Zigzag Level Order Traversal 解答
Question
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]
]
Solution
Traditional way is to use two queues to implement level order traversal. Here, we just add a flag to indicate whether it's from left to right or from right to left.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (root == null)
return result;
// Set a flag to help judge traversal sequence
// flag = 0, from left to right; flag = 1, from right to left
int flag = 0;
List<TreeNode> current = new ArrayList<TreeNode>();
List<TreeNode> next;
current.add(root); while (current.size() > 0) {
List<Integer> oneRecord = new ArrayList<Integer>();
next = new ArrayList<TreeNode>();
for (TreeNode tmpNode : current) {
if (tmpNode.left != null)
next.add(tmpNode.left);
if (tmpNode.right != null)
next.add(tmpNode.right);
if (flag == 0)
oneRecord.add(tmpNode.val);
else
oneRecord.add(0, tmpNode.val);
}
result.add(oneRecord);
current = next;
flag = 1 - flag;
}
return result;
}
}
Binary Tree Zigzag Level Order Traversal 解答的更多相关文章
- 【leetcode】Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- 剑指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 ...
- 【LeetCode】103. Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- [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解题报告—— 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 :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)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
随机推荐
- bzoj1379 [Baltic2001]Postman
Description 邮递员每天给N个村子的人送信,每个村子可能在某个十字路口上,或一条路的中央. 村子里的人都希望早点收到信,因此与邮递员达成一个协议:每个村子都有一个期望值Wi,如果这个村子是邮 ...
- UVa1399.Ancient Cipher
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Java I/O 模型的演进
什么是同步?什么是异步?阻塞和非阻塞又有什么区别?本文先从 Unix 的 I/O 模型讲起,介绍了5种常见的 I/O 模型.而后再引出 Java 的 I/O 模型的演进过程,并用实例说明如何选择合适的 ...
- Hibernate框架后续
持久化对象的唯一标识OID 1:我们都知道,在java中按照内存地址来区分同一个类的不同对象 而关系数据库按照主键来区分一条记录 在Hibernate中使用OID来建立内存中的对象和数据 ...
- python之路-pip安装
pip类似RedHat里面的yum,安装Python包非常方便 安装pip方法: 1.安装环境:ubuntu-14.04.2 sudo apt-get install python-pip pyt ...
- mysql 自己定义存储过程和触发器
mysql 自己定义存储过程和触发器 --存储过程示范 DROP PROCEDURE IF EXISTS PRO_TEST; CREATE PROCEDURE PRO_TEST(IN NUM_IN I ...
- JavaScript获取某年某月的最后一天
JavaScript获取某年某月的最后一天 1.实现源代码 <!DOCTYPE html> <!-- To change this license header, choose Li ...
- 涂抹Oracle笔记2:数据库的连接-启动-关闭
一.数据库的连接sqlplus <username>[/<password>][@<connect_idertifier>]|/[as sysdba| as sys ...
- 一般处理程序、ASP.NET核心知识(5)--转载
初窥 1.新建一个一般处理程序 新建一个一般处理程序 2.看看里头的代码 public class MyHandler : IHttpHandler { public void ProcessRequ ...
- 《JavaScript 闯关记》之基本包装类型
为了便于操作基本类型值,JavaScript 还提供了3个特殊的引用类型:Boolean.Number 和 String.实际上,每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象 ...