Binary Tree Zigzag Level Order Traversal

[LeetCode]

https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/

Total Accepted: 44275 Total Submissions: 165753 Difficulty: Medium

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).

Examples

For example:
Given binary tree {3,9,20,#,#,15,7},

     3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

[
[3],
[20,9],
[15,7]
]

Ways

方法一

使用队列的方法。

学习了java队列的使用。在一个队列中,放入第一个元素,取出后,放入左右孩子,再根据左右孩子逐个放入其后相应的孩子。队列为空的时候,说明已经遍历完成。

这个方法非常好,学习了。

加入标志符,根据标识符 来判断本层是否倒转

倒序算法:

    Collections.reverse(result);

方法二

使用栈的方法。

使用两个栈,一个栈放本层的节点,另外一个栈放下一层的栈。节点值加入List之后,将两个栈互换 。这个方法中要根据本层是否要倒序,判断是否颠倒加入栈中。

栈的主要方法:

    currLevel.push(root);
TreeNode node = currLevel.pop();

Solution

托管在我的GitHub上:

https://github.com/fuxuemingzhu/ZigzagLevelOrder

Captures

测试结果截图:

Reference

http://www.jiuzhang.com/solutions/binary-tree-zigzag-level-order-traversal/

Date

2015/10/14 23:34:11

【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告的更多相关文章

  1. [leetcode]Binary Tree Zigzag Level Order Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tr ...

  2. [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 ...

  3. [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 ...

  4. LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  5. [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 ...

  6. 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 ...

  7. leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

  8. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

  9. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

随机推荐

  1. 解决Package is not available (for R version XXX)?

    目录 1. 更新R(不推荐) 2. 更改或指定镜像源 3.源码安装 安装R包时这个错误是经常见到的.我认为有几个方法可解决,记录之. 1. 更新R(不推荐) 简单粗暴的方法就是更新R,但这波及的范围太 ...

  2. php header下载文件 无法查看原因

    php header下载文件 无法查看原因 php header下载文件 下方函数可以下载单个文件 function download($file_url){ if(!isset($file_url) ...

  3. centos yum安装mongodb,php扩展

    一,安装mongodb,php扩展 ? 1 [root@localhost ~]# yum install php-pecl-mongo mongodb mongodb-devel mongodb-s ...

  4. Hadoop入门 集群崩溃的处理方法

    目录 集群崩溃的处理方法 搞崩集群 错误示范 正确处理方法 1 回到hadoop的家目录 2 杀死进程 3 删除每个集群的data和logs 4 格式化 5 启动集群 总结 原因分析 集群崩溃的处理方 ...

  5. 修改linux文件权限命令:chmod 转载至 Avril 的随笔

    Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而禁 ...

  6. Linux学习 - 变量测试与内容替换

    变量置换方式 变量y没有设置 变量y为空 变量y有值 x=${y-新值} x=新值 x空 x=$y x=${y:-新值} x=新值 x=新值 x=$y x=${y+新值} x空 x=新值 x=新值 x ...

  7. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

  8. 访问网页全过程,用wireshark抓包分析

    用wireshark抓包查看访问网站过程 打开wireshark,打开一个无痕浏览器,输入网址,到网页呈现这一过程,网络数据包传递的消息都会被放在wireshark里.针对这些包,我们可以逐一分析,摸 ...

  9. OSGI 理论知识

    下面列出了主要的控制台命令: 表 1. Equinox OSGi 主要的控制台命令表 类别 命令 含义 控制框架 launch 启动框架 shutdown 停止框架 close 关闭.退出框架 exi ...

  10. Mybatis通用Mapper介绍和使用

    Mybatis通用Mapper介绍与使用 前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL. ...