http://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void print(node *node) {
if (!node) return;
print(node->left);
cout << node->data << " ";
print(node->right);
} void printspiral(node* root) {
if (!root) return;
bool flag = true;
stack<node*> S, T;
S.push(root);
while (!S.empty()) {
while (!S.empty()) {
node *top = S.top();
S.pop();
cout << top->data << " ";
if (!flag) {
if (top->left) T.push(top->left);
if (top->right) T.push(top->right);
}
else {
if (top->right) T.push(top->right);
if (top->left) T.push(top->left);
}
}
swap(S, T);
flag = !flag;
}
} int main() {
struct node* root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
root->right->right = new node();
printspiral(root);
return ;
}

Data Structure Binary Tree: Level order traversal in spiral form的更多相关文章

  1. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  2. [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)

    描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...

  3. 2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】

    Binary Tree Level Order Traversal 本题收获: 1.vector<vector<int>>的用法 vector<vector<int ...

  4. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

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

  5. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

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

  6. 【leetcode】Binary Tree Level Order Traversal I & II

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

  7. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  8. 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...

  9. 【Binary Tree Level Order Traversal II 】cpp

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

随机推荐

  1. ElasticSearch查询max_result_window问题处理

    需要出一份印地语文章的表,导出规则为: 1.所有印地语(包含各种颜色,各种状态)的文章 2.阅读数大于300 3.按照阅读推荐比进行排序,取前3000篇文章 说明: 1.文章信息,和阅读推荐数量在两个 ...

  2. Octave入门基础

    Octave入门基础 一.简单介绍 1.1 Octave是什么? Octave是一款用于数值计算和画图的开源软件.和Matlab一样,Octave 尤其精于矩阵运算:求解联立方程组.计算矩阵特征值和特 ...

  3. AutoCAD如何批量设置线宽

    1 如下图所示,全部选中图形,然后设置线宽   2 但是一般剖面线并不需要这么宽,我们打印预览可以发现完全黑的看不清了.   3 把线宽都设为0.2毫米效果不错

  4. 【SharePoint】SharePoint 2013 使用PreSaveAction自定义客户端验证

    使用PreSaveAction函数实现客户端自定义验证. 例:[项目编号]为空时,必须填写[责任者]项.(其中[项目编号]为单行文本框,[责任者]为用户/组选择框.) function PreSave ...

  5. Free Code Camp社区对数百计编程学习者进行的统计希望告诉你什么?

    文章来源:https://www.sdk.cn/news/5044 著名编程学习社区Free Code Camp对超过1.5万名编程学习者进行了调查.其中有一个问题为:“你对哪个编程职位最感兴趣?”有 ...

  6. 记录MySQL运行的SQL

    对照Oracle功能去学习Mysql总会发现亮点 Oracle中通过日志挖掘这一技能,能够找到以前运行过的全部记录: Mysql中也提供了3种方法{验证过的,我会记录详细做法} 方法1:{已验证} 记 ...

  7. VueJS事件处理器v-on

    事件监听可以使用 v-on 指令. v-on:click表达式 HTML: <!DOCTYPE html> <html> <head> <meta chars ...

  8. SQLSERVER 2008 链接 到 ORACLE 11

    MSSQL2008R2 链接 ORACLE 11: 创建链接: exec sp_addlinkedserver 'DBLINK_ORACL' , 'ORACLE' , 'MSDAORA' , 'ORC ...

  9. TI C66x DSP 系统events及其应用 - 5.9(IST重定位)

    RESET中断的获取包必须位于0地址处,然后其它中断的获取包能够位于不论什么256字(1k字节边界)对齐的程序空间.IST的位置由ISTP寄存器中的ISTB字段决定. IST又一次定位举例: 1,将I ...

  10. 淘宝数据库OceanBase SQL编译器部分 源代码阅读--解析SQL语法树

    OceanBase是阿里巴巴集团自主研发的可扩展的关系型数据库,实现了跨行跨表的事务,支持数千亿条记录.数百TB数据上的SQL操作. 在阿里巴巴集团下,OceanBase数据库支持了多个重要业务的数据 ...