[抄题]:

给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行)

[思维问题]:

不知道反复切换要怎么做:用boolean normalOrder当作布尔型控制变量

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

root一个点正常出入。第二层先左后右,出来就变成先右后左了。

[一刷]:

  1. 层遍历是放在原来的queue中,但是zigzag遍历的左右节点是放在下一层,另外一个栈next中。不要搞错
  2. 栈交换也是循环操作的,栈交换需要放在循环体里面

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

用两个栈:可以实现逆序,来回交换

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

层遍历

public class Solution {
/*
* @param root: A Tree
* @return: A list of lists of integer include the zigzag level order traversal of its nodes' values.
*/
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
//define
Stack<TreeNode> curt = new Stack<TreeNode>();
Stack<TreeNode> next = new Stack<TreeNode>();
Stack<TreeNode> temp = new Stack<TreeNode>();
List<List<Integer>> result = new ArrayList<List<Integer>>();
boolean normalOrder = true; if (root == null) {
return result;
} //put into stack
curt.push(root);
while (!curt.isEmpty()) {
List<Integer> level = new LinkedList<Integer>();
int size = curt.size();
for (int i = 0; i < size; i++) {
TreeNode node = curt.pop();
level.add(node.val);
if (normalOrder == true) {
if (node.left != null) {
next.push(node.left);//
}
if (node.right != null) {
next.push(node.right);
}
}
else {
if (node.right != null) {
next.push(node.right);
}
if (node.left != null) {
next.push(node.left);
}
}
}
result.add(level);
//reverse stack
temp = curt;
curt = next;
next = temp;
normalOrder = !normalOrder;//
} //output result
return result;
}
}

二叉树的锯齿形层次遍历 · Binary Tree Zigzag Level Order Traversal的更多相关文章

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

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

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

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

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

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

  5. Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树

    题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...

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

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

  8. 【leetcode】Binary Tree Zigzag Level Order Traversal

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

  9. 【LeetCode】103. Binary Tree Zigzag Level Order Traversal

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

随机推荐

  1. RF安装

    参考: http://www.cnblogs.com/zlj1992/p/6357373.html https://github.com/robotframework/RIDE/wiki/Instal ...

  2. linux下给php安装curl、gd(ubuntu)

    安装方法很简单,只需要一条命令. # sudo apt-get install curl libcurl3 libcurl3-dev php5-curl 恭喜,PHP5 cURL安装完毕.记得重启Ap ...

  3. unbound域名解析

    安装unbound服务 # yum install unbound -y 开启服务 linux系统如何查看命令属于哪一个安装包 # yum provides */netstat 安装netstat命令 ...

  4. 在 Linux 下使用mdadm创建 RAID 5

    在 RAID 5 中,数据条带化后存储在分布式奇偶校验的多个磁盘上.分布式奇偶校验的条带化意味着它将奇偶校验信息和条带化数据分布在多个磁盘上,这样会有很好的数据冗余. 在 Linux 中配置 RAID ...

  5. python入门-存储数据

    这里的存储数据使用json格式 json   是javascript object notation的意思  javascript的对象标记 1 写入 json.dump import json nu ...

  6. python实现一个栏目的分页抓取列表页抓取

    python实现一个栏目的分页抓取列表页抓取 #!/usr/bin/env python # coding=utf-8 import requests from bs4 import Beautifu ...

  7. CentOS Apache配置详解

    要想在linux上实现网页服务器(www)需要Apache这个服务器软件,不过Apache仅能提供最基本的静态网站数据而已,想要实现动态网站的话,最好还是要PHP与MySQL的支持,所以下面我们将会以 ...

  8. VB 调用动态链接库

    作为一种简单易用的Windows开发环境,Visual Basic从一推出就受到了广大编程人员的欢迎.它使 程序员不必再直接面对纷繁复杂的Windows消息,而可以将精力主要集中在程序功能的实现上,大 ...

  9. Linux SWAP 交换分区配置说明(转)

    一.SWAP 说明 1.1 SWAP 概述 当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被 ...

  10. Mysql 2条记录 差值计算

    1 表结构 2:  其实 是2个相同的 表根据rownum= rownum-1 来计算,所以先了解单个表的查询 附上SQL: #查询出1天的数据升序 ) as rownum, info.equipme ...