题目:

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

思路:

首先肯定的是要对树进行层序遍历,但是相邻两层的元素遍历顺序是相反的,因此传统的非递归遍历方法用一个队列肯定是无法实现。我们用两个栈来实现,同一层元素都在同一个栈中,相邻的两层元素放在不同的栈中,比如第一层元素放在第一个栈,那么第二层就放在第二个栈,第三层再放在第一个栈......如果某一层元素是从左往右遍历的,那么这层元素的孩子节点入栈顺序就是先左孩子后右孩子,相反如果某一层元素是从右往左遍历的,入栈顺序就是先右孩子后左孩子。

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var zigzagLevelOrder = function(root) {
var res=[],tempres=[];
if(root==null){
return res;
}
var s1=[],s2=[];
s1.push(root);
var flag=true; while(s1.length!=0||s2.length!=0){
var p=null; if(flag){
p=s1.pop();
tempres.push(p.val);
if(p.left){
s2.push(p.left);
}
if(p.right){
s2.push(p.right);
}
if(s1.length==0){
var temp=[];
for(var i=0,len=tempres.length;i<len;i++){
temp[i]=tempres[i];
}
res.push(temp);
tempres.length=0;
flag=false;
}
}else{
p=s2.pop();
tempres.push(p.val);
if(p.right){
s1.push(p.right);
}
if(p.left){
s1.push(p.left);
}
if(s2.length==0){
var temp=[];
for(var i=0,len=tempres.length;i<len;i++){
temp[i]=tempres[i];
}
res.push(temp);
tempres.length=0;
flag=true;
}
}
} return res;
};

【树】Binary Tree Zigzag Level Order Traversal的更多相关文章

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

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

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

  4. 【leetcode】Binary Tree Zigzag Level Order Traversal

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

  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】103. Binary Tree Zigzag Level Order Traversal

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

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

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

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

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

  10. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

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

随机推荐

  1. python读取文件另存为

    fr = open(filename_r,encoding='cp852') w2 = open(filename_w,'a')#a代表追加 w代表重写 for line in fr: w2.writ ...

  2. 团队项目(第四周之一)—GG队

    Alpha认领任务: 叶尚文:对应键盘监听结果的动画以及计算 于泽浩:制作背景gif图,并保证能在程序中循环播放 龙剑初:项目进度跟进及博客更新 杜婷萱:把图片结合起来,设置云朵透明度的变化 蔡晓晴: ...

  3. [翻译]NUnit---Property and Random Attributes(十四)

    小记:由于工作琐碎,没得心情翻译而且也在看<CLR vis C#>,所以断更了差不多5个月,现在继续翻译,保证会翻译完成,不会虎头蛇尾. 另:NUnit已经更新到2.6.3版本,虽然正在开 ...

  4. ABP框架中微服务跨域调用其它服务接口

    AjaxResponse为ABP自动包装的JSON格式 /// <summary> /// 通过地址和参数取得返回OutPut数据 /// </summary> /// < ...

  5. Spring Boot - Spring Data

    使用JPA 虽然JPA是一个标准,但spring中一般就是使用hibernate实现的 使用JPA(Java Persistence API,Java持久化API,是个规范,其实是借助Hibernat ...

  6. day85 ModuleForm Form组件

    1 forms组件与modelform组件 forms组件: https://www.cnblogs.com/yuanchenqi/articles/9036474.htmlmodelForm组件:h ...

  7. python numpy 数组中元素大于等于0的元素

    >>> import numpy as np >>> a = np.random.randint(-5, 5, (5, 5)) >>> a arr ...

  8. 在QtCreater中配置Artistic Style格式化Qt程序源代码!!

    Qt很吸引人,可能是我对Qt开发工具QtCreater不熟悉,只发现里面提供了一个快捷键:"ctrl+i",很多人说这就是格式化代码快捷键,我发现这仅仅是代码缩进,并不是真正意义上 ...

  9. BZOJ3775: 点和直线(计算几何+拉格朗日乘数法)

    题面 传送门 题解 劲啊-- 没有和\(Claris\)一样推,用了类似于\(Shinbokuow\)推已知点求最短直线的方法,结果\(WA\)了好几个小时,拿\(Claris\)代码拍了几个小时都没 ...

  10. 一,memcached的基本概念

    1,memcached的基本概念 memcached是一个高效的分布式内存对象缓存系统,它可以支持把各种php的数据(array,对象,基本数据类型)放入到它管理的内存中.简单的说,memcached ...