#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. 建立第一个Django工程---linux中的python

    建立第一个Django工程 环境: ip: 192.168.0.92 系统:centos7.5 安装django pip install django 启动一个HelloWorld工程 django- ...

  2. Ubuntu 18.04 下 PostgreSQL 10 的安装与基础配置

    下载安装 在命令行执行如下语句: apt-get install postgresql-10 该指令会帮助你下载如下PostgreSQL组件: name |explain | ------------ ...

  3. CBES = component-based entity system

    比较好的介绍 CBES 的文章 http://www.richardlord.net/blog/what-is-an-entity-framework

  4. Adobe Acrobat 如何通过书签制作多级目录

    废话不多说,直接上官方文档 看不清可 右击 > 在新标签页中打开图片

  5. beego conf配置文件

    1. 多个配置文件通过include引入 自定义配置文件mysql.conf 在app.conf 中引入mysql.conf include "mysql.conf"

  6. Sightseeing tour 【混合图欧拉回路】

    题目链接:http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total ...

  7. 原生js实现图片的3d效果

    <!doctype html><html lang="en"><head><meta charset="UTF-8"& ...

  8. js中的三目运算符详解

    判断 javascript中的三目运算符用作判断时,基本语法为: expression ? sentence1 : sentence2 当expression的值为真时执行sentence1,否则执行 ...

  9. win7 配置 用户/系统DSN (解决plsql odbc导入器 DSN 没有选项)

    (2013-05-07 15:07:29) 转载▼ 标签: it   老笔记本越来越卡,最近重新做了win7的系统 从sql server中导出的 excel文件 准备通过plsql倒入的oracle ...

  10. jupyter lab 的基本使用

    在创建一个文件即可 进入创建的文件,在创建一个ipynb文件即可操作 注意右上角必须是python3 可以哦(如果点了shutdown 就会没有内核 需要自己在定义python编辑器) jupyter ...