c++ 二叉树的遍历(迭代,递归)
#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++ 二叉树的遍历(迭代,递归)的更多相关文章
- 【Java】 二叉树的遍历(递归与循环+层序遍历)
在[Java] 大话数据结构(9) 树(二叉树.线索二叉树)一文中,已经实现了采用递归方法的前.中.后序遍历,本文补充了采用循环的实现方法.以及层序遍历并进行了一个总结. 递归实现 /* * 前序遍历 ...
- 二叉树的遍历(递归,迭代,Morris遍历)
二叉树的三种遍历方法: 先序,中序,后序,这三种遍历方式每一个都可以用递归,迭代,Morris三种形式实现,其中Morris效率最高,空间复杂度为O(1). 主要参考博客: 二叉树的遍历(递归,迭代, ...
- LeetCode 链表2_27+二叉树的遍历(递归与非递归)
---恢复内容开始--- 19. 删除链表的倒数第N个节点 实现原理:设置两个指针p,q,初始时先让p走n步,之后p与q一起走,当p走到结尾的时候,删除p.next即可. public ListNod ...
- JAVA二叉树递归构造、二叉树普通遍历及递归遍历
二叉树类: package com.antis.tree; public class BinaryTree { int data; //根节点数据 BinaryTree left; //左子树 Bin ...
- 数据结构 - 二叉树的遍历(递归VS非递归)
import java.util.LinkedList; public class BinaryTree { public static void main(String[] args) { int ...
- python二叉树的遍历,递归和非递归及相关其它
# encoding=utf-8class node(object): def __init__(self,data,left=None,right=None): self.data = data s ...
- C语言 二叉树的遍历(递归和非递归)
#include <iostream> #include <cstdio> #include "biTree.h" #include "cstdl ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
- 二叉树的遍历(递归,迭代,Morris遍历)
二叉树的遍历: 先序,中序,后序: 二叉树的遍历有三种常见的方法, 最简单的实现就是递归调用, 另外就是飞递归的迭代调用, 最后还有O(1)空间的morris遍历: 二叉树的结构定义: struct ...
随机推荐
- Ubuntu之安装PXE+Kickstart无人值守安装操作系统
CentOS安装PXE见 https://www.cnblogs.com/minseo/p/10774030.html 本文介绍Ubuntu系统安装pxe 1,环境查看 服务器ip地址:192.168 ...
- 利用工具破解HTTP身份验证的多种方法
https://www.hackingarticles.in/multiple-ways-to-exploiting-http-authentication/ 1)场景 利用Apache配置HTTP验 ...
- iOS-意见反馈UITextView的使用+不能输入字符输入
@interface DMFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>@property (nonat ...
- HDU 1087 最大递增子序列
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Flutter 实现简单搜索功能
先建立一个主文件,继承StatelessWidget,然后在home属性中加入SearchBarDemo,这是一个自定义的Widget,主要代码都在这个文件中. import 'package:flu ...
- 在HTML5 中使用 kindeditor 的方法
1.打开:http://kindeditor.net/ke4/examples/default.html 2.查看源代码,另存为 3.打开http://kindeditor.net/demo.php, ...
- eNSP——Hybrid接口的应用
原理: Hybrid接口既可以连接普通终端的接入链路又可以连接交换机间的干道链路,它允许多个VLAN的帧通过,并可以在出接口方向将某些VLAN帧的标签剥掉. Hybrid接口处理VLAN帧的过程如下: ...
- 微信小程序的calc不生效处理
大致文字初略的记录描述一下问题:外层是relative相对定位,内部一个view 需要绝对定位bottom的值为128rpx,同时还要兼容适配苹果x的底部,所以值是这样设置的: bottom: cal ...
- [TCP/IP] 滑动窗口
什么是滑动窗口? 滑动窗口机制是TCP协议的一种流量控制和防拥塞的机制. 滑动窗口的工作原理? 简单来讲,就是接收方和发送方分别保留一块缓冲区,作为接收和发送数据来使用,发送数据过程中,如果发送方发的 ...
- 剑指offer47:位运算+递归。求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
1 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 2 思路和方法 (1)递归,不能使用if等 ...