#include <iostream>
#include <cstdio>
#include <vector>
#include <stack> #define ls(x) (((x + 1)<<1) - 1)
#define rs(x) ((x + 1)<<1) using namespace std; const int NULLVAL = -; typedef struct Node{
struct Node *left;
struct Node *right;
int val;
}TNode, * PNode; typedef struct Node2{
PNode btNode;
bool isFirst;
Node2(){
isFirst = true;
}
}traNode, * PTraNode; void createBTree(PNode rt, vector<int>v, int cur)
{
rt -> val = v[cur];
if (ls(cur) < v.size() && v[ls(cur)] != NULLVAL){
PNode newNode = new TNode();
rt -> left = newNode;
createBTree(rt -> left, v, ls(cur));
}if (rs(cur) < v.size() && v[rs(cur)] != NULLVAL){
PNode newNode = new TNode();
rt -> right = newNode;
createBTree(rt -> right, v, rs(cur));
}
} void travelTree(PNode rt)
{
if(rt){
travelTree(rt -> left);
cout<<"node: "<<rt -> val<<endl;
travelTree(rt -> right);
}
} void travelWithoutRecursiveInorder(PNode rt)
{
if(rt == NULL) return ;
PNode p = rt;
stack<PNode>sta; while(!sta.empty() || p != NULL){ while(p != NULL){
sta.push(p);
p = p -> left;
}
if( !sta.empty()){
p = sta.top();
sta.pop();
cout<<"val: "<<p->val<<endl;
p = p -> right;
}
}
} void travelWithoutRecursivePreorder(PNode rt)
{
if(rt == NULL) return ;
PNode p = rt;
stack<PNode>sta;
while(p || sta.empty() == false){
while(p){
sta.push(p);
cout<<"val: "<<p->val<<endl;
p = p -> left;
}
if(!sta.empty()){
p = sta.top();
sta.pop();
p = p -> right;
}
}
} /*
* Postorder后序遍历骚味复杂一点点,因对于任意一个节点需要先访问其left child 然后访问 其right child 最后第二次访问到该节点
* 才能输出该点值.所以 需要再另行设计一个数据结构 traNode 里面封装了{TreeNode 和 isFirst(一个标记用来指示这个节点是否是第一次访问到)}
* 只有第二次访问到才会输出该值.
* 其实还有第二种方法:
*/
void travelWithoutRecursivePostorder(PNode rt)
{
if(rt == NULL) return;
stack<PTraNode>sta;
PNode p = rt;
while(p || !sta.empty()){
while(p){
PTraNode ptn = new traNode();
ptn ->btNode = p;
sta.push(ptn);
p = p -> left;
}
if(!sta.empty()){
PTraNode ptn = sta.top();
sta.pop();
if(ptn->isFirst ){
ptn->isFirst = false;
sta.push(ptn);
p = ptn->btNode -> right;
}else{
cout<<"val: "<<ptn->btNode->val<<endl;
p = NULL;
}
}
}
}
int main()
{
freopen("in", "r", stdin);
PNode root = new TNode();
int n;
vector<int>v;
cin>>n;
for(int i = ; i < n; i ++){
int r;
cin>>r;
v.push_back(r);
}
v.push_back(NULLVAL);
createBTree(root, v, );
//travalTree(root);
cout<<"Preorder:"<<endl;
travelWithoutRecursivePreorder(root);
cout<<"Inorder"<<endl;
travelWithoutRecursiveInorder(root);
cout<<"Postorder"<<endl;
travelWithoutRecursivePostorder(root);
return ;
}

《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)的更多相关文章

  1. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  2. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  3. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. c/c++二叉树的创建与遍历(非递归遍历左右中,破坏树结构)

    二叉树的创建与遍历(非递归遍历左右中,破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...

  8. Java实现二叉树的创建和遍历操作(有更新)

    博主强烈建议跳过分割线前面的部分,直接看下文更新的那些即可. 最近在学习二叉树的相关知识,一开始真的是毫无头绪.本来学的是C++二叉树,但苦于编译器老是出故障,于是就转用Java来实现二叉树的操作.但 ...

  9. 二叉树的创建、遍历(递归和非递归实现)、交换左右子数、求高度(c++实现)

    要求:以左右孩子表示法实现链式方式存储的二叉树(lson—rson),以菜单方式设计并完成功能任务:建立并存储树.输出前序遍历结果.输出中序遍历结果.输出后序遍历结果.交换左右子树.统计高度,其中对于 ...

随机推荐

  1. ipython

    ipython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数. T ...

  2. IIS-反向代理

    测试环境:Windows10.IIS/10.0 1.安装ARR.URL Rewrite(URL重写工具2.0) 注意英文和中文环境的对应: Application Request Routing 对应 ...

  3. HTML5 history新特性pushState、replaceState,popstate

    http://blog.csdn.net/tianyitianyi1/article/details/7426606 https://developer.mozilla.org/zh-CN/docs/ ...

  4. POI中getLastRowNum() 和getLastCellNum()的区别

    hssfSheet.getLastRowNum();//最后一行行标,比行数小1 hssfSheet.getRow(k).getLastCellNum();//获取列数,比最后一列列标大1

  5. Java加密算法 RSA

    Java加密算法 RSA 2015-06-06 08:44 511人阅读 评论(0) 收藏 举报  分类: JAVA(57)  公钥加密也称为非对称加密.速度慢.加密和解密的钥匙不相同,某一个人持有私 ...

  6. 【转】Reactor与Proactor两种模式区别

    转自:http://www.cnblogs.com/cbscan/articles/2107494.html 两种IO多路复用方案:Reactor and Proactor 一般情况下,I/O 复用机 ...

  7. [Eclipse] - 集成Tomcat热加载插件

    使用Eclipse + Tomcat,要使用热加载,总是会重启tomcat webapp. 可以使用这个插件:jrebel 如果是Tomcat 7.0+版本,需要使用jrebel5.5.1+的版本,不 ...

  8. 一篇关于SpringMVC 传统文件上传的方法

    一.界面效果 二.html代码 <legend>上传APK文件</legend> <form action="<%=basePath%>/apks/ ...

  9. 适合最新版docker自定义启动配置

    docker不断发布新版本,以前默认的在 /etc/default/docker里修改,但是新版已经不推荐了 注意: 一些文章推荐在 /lib/systemd/system/docker.servic ...

  10. VG vs SS WE vs IM [20160815]

    上单:慎,纳尔,艾克,艾瑞莉娅,普朗克 中单:弗拉基米尔,玛尔扎哈,卡尔玛,丽桑卓,索尔,崔斯特,辛德拉 打野:雷克赛,奈德丽,古拉加斯,伊莉丝,赫卡里姆,玛尔扎哈 下路:艾希,克格莫,烬,希维尔,布 ...