#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. JSBinding+SharpKit / 更新的原理

    首先,其实不是热更新,而是更新. 热更新意思是不重启游戏,但只要你脚本里有存储数据,就不可能.所以只能叫更新. 但大家都这么说,所以... 先举个具体的例子: 如果是C#:在 Prefab 的 Gam ...

  2. js中的this指针(三)

    当一个函数并非一个对象的忏悔时,它会被当作一个函数来调用. 此时,函数中的 this 指针被绑定到了全局对象. 后果:方法不能利用内部函数来帮助工作,由于 this 被绑定了错误的值,将无法共享该方法 ...

  3. nunjucks.js模板渲染

    直接用 script 引入文件: <script src="nunjucks.js"></script> 是使用 render 来直接渲染文件,这种方式支持 ...

  4. 【转载】解决方案:Resharper对系统关键字提示‘can not resolve symbol XXX’,并且显示红色,但是编译没有问题

    环境:Visual studio 2013 community Update 4 + Resharper 8.2 + Windows 7 现象: 我的C#工程编译没有问题, 但是在代码编辑器中系统关键 ...

  5. Centreon 监控报警

    1.系统更新:yum update 2.安装组件:yum install -y httpd php-pear php php-mysql php-gd php-ldap php-xml php-mbs ...

  6. VS2013的一些常用快捷键

    1.回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合键“Ctrl + -”: 2)前进到下一个光标位置:“Ctrl + Shift + - ”. 2.复制/剪切/删除整行代码 ...

  7. ios xmpp开发应用后台模式接收聊天信息处理方案

    ios xmpp开发应用后台模式接收聊天信息 最近在使用xmppframwork来实现一个聊天应用,碰到了一个问题,应用进入后台以后,就接收不到消息了: 怎么样才能使应用被切到后台时,应用中的网络连接 ...

  8. http://www.iis.net/downloads/microsoft/url-rewrite

    http://www.iis.net/downloads/microsoft/url-rewrite iis  url重写模块.官方下载

  9. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  10. 在autoit中如何将combobox设置为只允许选择不允许输入呢

    在autoit中如何将combobox设置为只允许选择不允许输入呢?只需要将设置style    $CBS_DROPDOWNLIST,默认的是$CBS_DROPDOWN既能输入也能选择.代码设置如下: ...