#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. js 高级程序设计 第三章学习笔记——Number数据类型需要注意的事项

    1.浮点数值 虽然小数点前面可以没有整数,但是并不推荐这种写法. 由于保存浮点数值需要的内存空间是保存整数值的两倍,因此ECMAScript会不失时机地将浮点数值转化为整数数值.显然,如果小数点后面没 ...

  2. 关于SVM的一些知识点

    SVM支持向量机 定义:支持向量机是主要用于解决分类问题的学习模型.它的基本模型是在特征空间中寻找间隔最大化的分离超平面的线性分类器. 分类 1-当训练样本线性可分,通过硬间隔最大化,学习一个线性分类 ...

  3. Ubuntu搭建Spring源码环境常见问题

    在一心想要学习Spring框架源码时,我们会遇到很多麻烦的问题.开始本文前,你只需要拥有一个装好IDEA的Ubuntu系统就可以愉快启程了.如果还没有IDEA,可以参考在Ubuntu上安装Intell ...

  4. (4)Linux命令分类汇总(13~16)

    Linux命令分类汇总(13~16) (十三)系统管理与性能监视命令(9个) 79       chkconfig 管理Linux系统开机启动项. 80       vmstat 虚拟内存统计. 81 ...

  5. Linear regression with one variable - Model representation

    摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第二章<单变量线性回归>中第6课时<模型概述>的视频原文字幕.为本人在视频学习过程中逐字逐句记录下 ...

  6. 使用请求包装器RequestWrapper 对博客内容进行编码

    1.写一个文章类 代码如下 package model; public class article { private int id; private String title; private St ...

  7. shell top

    top -bn1 它表示非动态打印系统资源使用情况,即一次性全部把所有信息输出出来而非动态显示,可以用在shell脚本中

  8. ssh出现公钥错误问题的解决方法

      问题:主机app1推送公钥时,公钥判定错误   原因:之前推过公钥,用的是ip而不是主机名(即hosts文件中的对应关系不对),导致app1的~/.ssh/known_hosts中的公钥对不上. ...

  9. [Agc029C]Lexicographic constraints_进制_二分答案_贪心

    Lexicographic constraints 题目链接:https://atcoder.jp/contests/agc029/tasks/agc029_c 数据范围:略. 题解: 二分是显然的, ...

  10. GitHub项目管理维护实用教程

    GitHub项目维护教程   1)注册GitHub账户并登陆: 2)在Windows cmd(或Ubuntu中的terminal)中cd到自己的工作目录,将仓库clone下来: 命令: git clo ...