#include<iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include<stack>
#include<ctime>
#include <sstream>
#include <queue>
using namespace std;
// 树节点的结构体
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 函数声明
void preorder (TreeNode * p);
void midorder (TreeNode *p);
void postorder(TreeNode *p);
void preorder_1 (TreeNode *p);
void preorder_2 (TreeNode *p);
void midorder_1(TreeNode* p);
void postorder_1(TreeNode* p);
void levelorder_1(TreeNode*p); int main()
{
TreeNode* a=new TreeNode (2);
TreeNode* b= new TreeNode(3);
TreeNode* c= new TreeNode(1);
a->left=b;a->right=c;
TreeNode* d= new TreeNode(6);
TreeNode* e= new TreeNode(4);
b->left=new TreeNode(5);b->right=d;
d->left=new TreeNode(3);d->right=new TreeNode(0);
c->right=e;
e->left=new TreeNode (-1);e->right=new TreeNode(2); cout << "递归版: " <<endl;
preorder(a);
cout << endl;
midorder(a);
cout << endl;
postorder(a);
cout << endl;
cout <<"迭代版: " <<endl;
cout << "先序遍历1.0:";
preorder_1(a);
cout <<endl;
cout << "先序遍历2.0:";
preorder_2(a);
cout <<endl;
cout << "中序遍历:";
midorder_1(a);
cout << endl;
cout <<"层次遍历:" ;
levelorder_1(a);
cout << endl;
return 0;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%% 递归版 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// 树的前序遍历
void preorder (TreeNode * p)
{
if(p == NULL) return; cout<< p->val << " ";
preorder(p->left);
preorder(p->right);
}
//树的中序遍历
void midorder (TreeNode *p)
{
if(p == NULL) return; midorder(p->left);
cout<< p->val << " ";
midorder(p->right);
}
// 树的后序遍历
void postorder(TreeNode *p)
{
if(p == NULL) return; postorder(p->left);
postorder(p->right);
cout<< p->val << " ";
}
// %%%%%%%%%%%%%%%%%%%%%%%% 迭代版 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// 前序遍历1.0
void preorder_1 (TreeNode *p)
{
stack<TreeNode *> s;
s.push(p);
while(!s.empty())
{
TreeNode* tmp=s.top();
s.pop();
cout << tmp->val << " ";
if(tmp->right != NULL) s.push(tmp->right);
if(tmp->left != NULL) s.push(tmp->left);
}
}
// 前序遍历2.0
void visitalongleft(TreeNode* p,stack<TreeNode*> &s)
{
while(p!=NULL)
{
s.push(p);
cout << p->val << " ";
p=p->left;
}
}
void preorder_2 (TreeNode *p)
{
stack<TreeNode*> s;
while(true)
{
visitalongleft(p,s);
if(s.empty()==true) break;
TreeNode* tmp=s.top();
s.pop();
p=tmp->right;
}
}
// 中序遍历迭代版
void leftalong(TreeNode* p,stack<TreeNode*> &s)
{
while(p!=NULL) {s.push(p);p=p->left;}
} void midorder_1(TreeNode* p)
{
stack<TreeNode*> s;
while(true)
{
leftalong(p,s);
if(s.empty()==true) break;
cout << s.top()->val << " ";
p=s.top()->right;
s.pop();
}
} void levelorder_1(TreeNode*p)
{
queue<TreeNode *> q;
if(p==NULL) return;
q.push(p);
while(!q.empty())
{
cout << q.front()->val <<" ";
if(q.front()->left != NULL) q.push(q.front()->left);
if(q.front()->right != NULL) q.push(q.front()->right);
q.pop();
}
}

  

c++ 二叉树的遍历(迭代,递归)的更多相关文章

  1. 【Java】 二叉树的遍历(递归与循环+层序遍历)

    在[Java] 大话数据结构(9) 树(二叉树.线索二叉树)一文中,已经实现了采用递归方法的前.中.后序遍历,本文补充了采用循环的实现方法.以及层序遍历并进行了一个总结. 递归实现 /* * 前序遍历 ...

  2. 二叉树的遍历(递归,迭代,Morris遍历)

    二叉树的三种遍历方法: 先序,中序,后序,这三种遍历方式每一个都可以用递归,迭代,Morris三种形式实现,其中Morris效率最高,空间复杂度为O(1). 主要参考博客: 二叉树的遍历(递归,迭代, ...

  3. LeetCode 链表2_27+二叉树的遍历(递归与非递归)

    ---恢复内容开始--- 19. 删除链表的倒数第N个节点 实现原理:设置两个指针p,q,初始时先让p走n步,之后p与q一起走,当p走到结尾的时候,删除p.next即可. public ListNod ...

  4. JAVA二叉树递归构造、二叉树普通遍历及递归遍历

    二叉树类: package com.antis.tree; public class BinaryTree { int data; //根节点数据 BinaryTree left; //左子树 Bin ...

  5. 数据结构 - 二叉树的遍历(递归VS非递归)

    import java.util.LinkedList; public class BinaryTree { public static void main(String[] args) { int ...

  6. python二叉树的遍历,递归和非递归及相关其它

    # encoding=utf-8class node(object): def __init__(self,data,left=None,right=None): self.data = data s ...

  7. C语言 二叉树的遍历(递归和非递归)

    #include <iostream> #include <cstdio> #include "biTree.h" #include "cstdl ...

  8. C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解

    剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...

  9. 二叉树的遍历(递归,迭代,Morris遍历)

    二叉树的遍历: 先序,中序,后序: 二叉树的遍历有三种常见的方法, 最简单的实现就是递归调用, 另外就是飞递归的迭代调用, 最后还有O(1)空间的morris遍历: 二叉树的结构定义: struct ...

随机推荐

  1. word内存不足 解决办法

    word2013内存不足 解决办法具体操作如下: 尝试了很多方法和经验表示都没有效果,最后找到个有效果的分享给大家: 点击“文件”——选项——加载项“   点左下的”管理“-” 转到“ 把加载项前面的 ...

  2. markdown居中对齐,左对齐,右对齐

    Markdown语法本身没有居中,但Markdown中支持基本的HTMl语法,可以使用HTML语法. 居中: <center>居中</center> 左对齐: <p al ...

  3. NMS(Non-Maximum Suppression) 非极大值抑制

    NMS  非极大值抑制:找到局部最大值,并删除邻域内其他的值. 简单说一下流程: 首先剔除背景(背景无需NMS),假设有6个边界框,根据分类置信度对这6个边界框做降序排列,假设顺序为A.B.C.D.E ...

  4. 大数据(bigdata)练习题

    1.在HDFS文件系统的根目录下创建递归目录“1daoyun/file”,将附件中的BigDataSkills.txt文件,上传到1daoyun/file目录中,使用相关命令查看文件系统中1daoyu ...

  5. 日志.VC

    1. int WriteLog(char* _pcFullFileName, char* _pcWrite, int _iWriteLen, unsigned long * _pdwWritten) ...

  6. Guava源码阅读-base-CharMatcher

    package com.google.common.base; (部分内容摘自:http://blog.csdn.net/idealemail/article/details/53860439) 之前 ...

  7. Encoding.GetEncoding 编码列表

    代码页 名称 显示名称   37 IBM037 IBM EBCDIC(美国 - 加拿大)   437 IBM437 OEM 美国   500 IBM500 IBM EBCDIC(国际)   708 A ...

  8. ajax提交 的编码小结

    今天用ajax的post方法提交数据给webservice,由于有email的缘故发现,注册方法调用总是报错,打开报文一看提交的 发现@符号被变成了%40 , abc@126.com 即abc%401 ...

  9. C#读写操作app.config中的数据

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <connecti ...

  10. linux内核exec过程

    简介 本文分析linux内核exec系统调用执行过程中可执行文件的加载过程和栈的设置,内核代码版本为2.6.32 分析 \arch\ia64\kernel\process.c中有sys_exec函数的 ...