[抄题]:

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

[思维问题]:

不知道反复切换要怎么做:用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. linux下给php安装curl、gd(ubuntu)

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

  2. Linux下安装Nginx依赖包和Nginx的命令

    1.安装依赖包pcrecd /usr/local/srcwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar ...

  3. NVMe on RHEL7

    原文地址https://www.dell.com/support/article/cn/zh/cnbsd1/sln312382/nvme-on-rhel7?lang=en Posted on beha ...

  4. solr6.3根据搜索关键词词频(关键词出现次数、关键词highlight)进行排序

    http://localhost:8080/solr/test/select?fq=product_name:大有&indent=on&q=product_name:大有电钻 OR r ...

  5. Mac上如何用命令修改文件内容

    首先打开iTerm,切到文件所在的文件夹目录下 cd xx 然后进入编辑模式 vim xx.xx 然后插入修改 shift + i 修改之后退出插入模式 esc 保存退出 shift + :  wq

  6. 微信公众号开发者模式自定义菜单 node

    纯属分享 var config = require('./admin/wx/config/config'); var API = require('wechat-api'); var api = ne ...

  7. head 标签

    1.<meta - > <meta charset="UTF-8"> #utf-8字符编码 <meta http-equiv="Refres ...

  8. as3 代码优化

    1 代码写法 1 定义局部变量 定义局部变量的时候,一定要用关键字var来定义,因为在Flash播放器中,局部变量的运行速度更快,而且在他们的作用域外是不耗占系统资源的.当一个函数调用结束的时候,相应 ...

  9. 15 MySQL--索引

    索引: http://www.cnblogs.com/linhaifeng/articles/7356064.html http://www.cnblogs.com/linhaifeng/articl ...

  10. H5特性回顾

    canvas 绘画, video 媒介回放 audio元素 对本地离线存储的更好支持, 新的特殊内容 - 元素 比如 article,footer,header,nav,section, 新的表单控件 ...