#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. centos7.5配置ntp时间服务器

    Centos7配置ntp时间服务器 Centos7配置时间服务器,一个服务端,多个客户端,客户端去同步服务端 服务端: 1.先修改时区,否则即使配置完ntp时间也不对,修改时区参照:https://w ...

  2. iOS——偏好设置的创建,数据写入与读取

    NSUserDefaults与NSDictinary? 应用通过NSUserDefaults用键值对的方式来读取和保存偏好设置数据,与通过键从NSDictionary对象中获取数据一样,不同之处在于N ...

  3. IDEA配置Hystrix过程中报错: java.lang.IllegalStateException: No instances available for user-service

    最近在练习微服务架构中, 使用IDEA配置完Hystrix, 添加熔断方法后, 在浏览器中访问未启动的( 含有熔断方法注解 )的路径时, 报出了 : 500: No instances availab ...

  4. 理解、学习与使用 JAVA 中的 Optional【转载】

    这是一篇转载的文章.刚学java的时候看了好久这个Optional,但一直是懵的.今天又又遇到了,重新回来再看的时候,发现并没有那么难道那个. 转载的文章再开头处写了一个对于理解Optional很关键 ...

  5. OpenGL.Qt551.问题

    1.Qt551 + vs2013 + Win7x64 缘由:将“教程14:渲染到纹理.html(http://www.opengl-tutorial.org/cn/intermediate-tutor ...

  6. 更新neo4j节点信息

    将多个属性的内容更新到节点上 def update_by_id(id,graph,**kwargs): """ 更新节点的属性 根据节点的ID来更新节点的属性,如果存在该 ...

  7. neo4j 将一个节点的属性复制到另一个节点上

    在使用Python操作Neo4j数据库的时候,经常会遇到重复的节点,需要将一个节点的属性复制到另一个节点,之后将该节点删除. def copy_node_properties(source_node_ ...

  8. 使用pycharm开发web——django2.1.5(三)创建models并进入交互界面shell做一些简单操作

    这里model可以认为是数据对象本身 相当于在写java代码时候model目录下创建的实体类,models.py 中可以包含多个实体类,感觉这个操作挺骚的 下面是polls app里面的models, ...

  9. Win7原装ISO镜像封装USB3.0&网卡驱动

    Win7原装ISO镜像封装USB3.0&网卡驱动   最新购买的电脑是Windows10系统,想装回Windows7,但是装Windows7发现网络适配器没出现,如果没有USB2.0接口,US ...

  10. 统计学习方法 | 感知机 | python实现

    感知机是二类分类的线性分类模型,利用随机梯度下降法对基于误分类的损失函数进行极小化. 书中算法可以将所有样本和系数向量写成增广向量的形式,并将所有负样本乘以-1,统一形式,方便计算. (1)训练数据集 ...