《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#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)的更多相关文章
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- c/c++二叉树的创建与遍历(非递归遍历左右中,破坏树结构)
二叉树的创建与遍历(非递归遍历左右中,破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...
- Java实现二叉树的创建和遍历操作(有更新)
博主强烈建议跳过分割线前面的部分,直接看下文更新的那些即可. 最近在学习二叉树的相关知识,一开始真的是毫无头绪.本来学的是C++二叉树,但苦于编译器老是出故障,于是就转用Java来实现二叉树的操作.但 ...
- 二叉树的创建、遍历(递归和非递归实现)、交换左右子数、求高度(c++实现)
要求:以左右孩子表示法实现链式方式存储的二叉树(lson—rson),以菜单方式设计并完成功能任务:建立并存储树.输出前序遍历结果.输出中序遍历结果.输出后序遍历结果.交换左右子树.统计高度,其中对于 ...
随机推荐
- LINK : fatal error LNK1117: syntax error in option 'VERSION:1.6.5'
今天在用vs2015编译ogre 1.6.5的代码时发生连接错误 LINK : fatal error LNK1117: syntax error in option 'VERSION:1.6.5'. ...
- Windows 10 解决 0x80070021 错误
Windows 10 已经不支持 aspnet_regiis -i. 启动和关闭Windows功能中安装 ".NET Framework 4.6 高级服务" 即可解决. 以下是借鉴 ...
- 20151214下拉列表:DropDownList
注意: .如果用事件的话就要把控件的AutoPostBack设置成true .防止网页刷新用一个判断 if (!IsPostBack)//判断是第一个开始还是取的返回值 { } 下拉列表:DropDo ...
- git秘钥配置--转
git是分布式的代码管理工具,远程的代码管理是基于ssh的,所以要使用远程的git则需要ssh的配置.github的ssh配置如下:一 .设置git的user name和email:$ git con ...
- MSSQL反旋转的例子
with cte as ( select 'A' as tag , as num_1 , as num_2 , as num_3 , as num_4 ,null as num_5 union sel ...
- shell问题(转)
一个同学问我一个问题,说有以下文件内容,要求输出为特定的格式.这里就献丑给出一个处理的方法吧,由于时间关系可能我的答案并不是最好的,但是我尽量将我的答案讲解明白,让你理解处理的方法.如果您有简单明了的 ...
- .NET微信自定义分享标题、缩略图、超链接及描述的设置方法
前端Js引用: <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> ...
- JS操作iframe
1. 获得iframe的window对象 存在跨域访问限制. chrome:iframeElement. contentWindow firefox: iframeElement.contentWin ...
- 报错:Unable to load configuration. - action - file:/E:/apache-tomcat-8.0.37/webapps/20161102-struts2-3/WEB-INF/classes/struts.xml:11:73
第一种报错: 严重: Exception starting filter struts2Unable to load configuration. - action - file:/E:/apache ...
- Android总结篇系列:Activity生命周期
Android官方文档和其他不少资料都对Activity生命周期进行了详细介绍,在结合资料和项目开发过程中遇到的问题,本文将对Activity生命周期进行一次总结. Activity是由Activit ...