Data Structure Binary Tree: Level order traversal in spiral form
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的更多相关文章
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)
描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...
- 2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】
Binary Tree Level Order Traversal 本题收获: 1.vector<vector<int>>的用法 vector<vector<int ...
- [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 ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【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, ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- 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 ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
随机推荐
- Vbs脚本经典教材
转载:http://www.cnblogs.com/BeyondTechnology/archive/2011/01/10/1932440.html Vbs脚本经典教材(最全的资料还是MSDN) —为 ...
- Jenkins安装war版本
Jenkins的war包安装很简单: 下载jenkins的war包地址:https://jenkins.io/download/ 选择对应的版本 然后放入tomcat启动就好,其他根据提示来就好,比较 ...
- 微信小程序 - 单个题目
后端传过来的数据,如果通过wx:for遍历出来那就是一个页面全部排下来.... 我的想法就是,页面初始化时设置一个默认值 1/50 就是 index+1 / 50(后端传过来的数组长度),通过控制in ...
- layui-字体图标
layui官网下载:GitHub:https://github.com/sentsin/layui/ layui官网首页-下载:http://www.layui.com/ layui-字体图标-官方网 ...
- LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
- Coursera上的machine learning学完啦
Coursera上的第一门公开课最终要结束啦-- 全部的代码http://download.csdn.net/detail/abcd1992719g/7306053 老师的Octave代码很赞.框架打 ...
- objc_msgSend 报错
NSMutableArray * mutableArray = [NSMutableArray arrayWithArray:array]; objc_msgSend(mutableArray,@se ...
- 获取input光标的x和y轴
http://blog.csdn.net/kingwolfofsky/article/details/6586029 index.html <!DOCTYPE html> <html ...
- Java源代码之LinkedHashMap
Java源代码之LinkedHashMap 转载请注明出处:http://blog.csdn.net/itismelzp/article/details/50554412 一.LinkedHashMa ...
- 多媒体开发之---h264中 的RTP PAYLOAD 格式
H.264 视频 RTP 负载格式 1. 网络抽象层单元类型 (NALU) NALU 头由一个字节组成, 它的语法如下: +---------------+ |0|1|2|3|4|5|6|7 ...