前序遍历 非递归

	public void preordernorec(TreeNode root){
//System.out.println("先序遍历(非递归):");
//用数组模拟栈,假设有节点个数不超过32个
TreeNode[] stack = new TreeNode[32];
for(int i =0;i<32;i++){
stack[i] = null;
}
int index =0;
TreeNode pnode = root;
while(pnode!=null||index>0){
while(pnode!=null){
System.out.print(pnode.getKey()+",");
stack[index++] = pnode;
pnode = pnode.getLeftchlid();
}
pnode = stack[--index];
pnode = pnode.getRightchild();
}
//System.out.println("");
}

前序遍历 递归

public void preorder(TreeNode root){

		if(root!=null){
System.out.print(root.getKey()+",");
preorder(root.getLeftchlid());
preorder(root.getRightchild());
}
}

中序遍历 非递归

	public void inordernorec(TreeNode root){
TreeNode[] stack = new TreeNode[32];
int index=0;
for(int i =0;i<32;i++){
stack[i] = null;
}
TreeNode pnode = root;
while(pnode!=null||index>0){
while(pnode!=null){
stack[index++] = pnode;
pnode = pnode.getLeftchlid();
}
pnode = stack[--index];
System.out.print(pnode.getKey()+",");
pnode = pnode.getRightchild();
} //System.out.println("");
}

中序遍历 递归

public void inorder(TreeNode root){

		if(root!=null){

			inorder(root.getLeftchlid());
System.out.print(root.getKey()+",");
inorder(root.getRightchild());
}
}

后序遍历 非递归

public void postordernorec(TreeNode root){
TreeNode[] stack = new TreeNode[32];
int index=0;
for(int i =0;i<32;i++){
stack[i] = null;
}
TreeNode pnode = root;
TreeNode LastVisit = null;
while(pnode!=null||index>0){
while(pnode!=null){
stack[index++] = pnode;
pnode = pnode.getLeftchlid();
}
pnode=stack[index-1];
if(pnode.getRightchild()==null||pnode.getRightchild()==LastVisit){
System.out.print(pnode.getKey()+",");
LastVisit = pnode;
index--;
pnode = null;
}
else
{
pnode = pnode.getRightchild();
}
}
}

后序遍历 递归

public void postorder(TreeNode root){
if(root!=null){
postorder(root.getLeftchlid());
postorder(root.getRightchild());
System.out.print(root.getKey()+",");
}
}

前序 中序 后序 遍历 递归 非递归算法 java实现的更多相关文章

  1. 二叉树(前序,中序,后序,层序)遍历递归与循环的python实现

    二叉树的遍历是在面试使比较常见的项目了.对于二叉树的前中后层序遍历,每种遍历都可以递归和循环两种实现方法,且每种遍历的递归实现都比循环实现要简洁.下面做一个小结. 一.中序遍历 前中后序三种遍历方法对 ...

  2. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  3. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. 前序+中序->后序 中序+后序->前序

    前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...

  5. 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树: SDUT 1489 Description  已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input  输入数据有多组,第一行是一个整数t (t& ...

  6. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  7. SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...

  8. 【C&数据结构】---关于链表结构的前序插入和后序插入

    刷LeetCode题目,需要用到链表的知识,忽然发现自己对于链表的插入已经忘得差不多了,以前总觉得理解了记住了,但是发现真的好记性不如烂笔头,每一次得学习没有总结输出,基本等于没有学习.连复盘得机会都 ...

  9. 【11】-java递归和非递归二叉树前序中序后序遍历

    二叉树的遍历 对于二叉树来讲最主要.最基本的运算是遍历. 遍历二叉树 是指以一定的次序访问二叉树中的每个结点.所谓 访问结点 是指对结点进行各种操作的简称.例如,查询结点数据域的内容,或输出它的值,或 ...

随机推荐

  1. Scrambled Polygon(斜率排序)

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7799   Accepted: 3707 ...

  2. Oracle 表三种连接方式(sql优化)

    在查看sql执行计划时,我们会发现表的连接方式有多种,本文对表的连接方式进行介绍以便更好看懂执行计划和理解sql执行原理. 一.连接方式: 嵌套循环(Nested Loops (NL)) (散列)哈希 ...

  3. js 模板引擎 jade使用语法

    Jade是一款高性能简洁易懂的模板引擎,Jade是Haml的Javascript实现,在服务端(NodeJS)及客户端均有支持. 功能 · 客户端支持 · 超强的可读性 · 灵活易用的缩进 · 块扩展 ...

  4. php怎样求一个数组中最长的

    <?php $arr = array( 0 => 'd', 1 => '68b3', 2 => 'a86', 3 => 'c9aa97b23b71d5c', 4 => ...

  5. jacksonall的使用,解析json

    转自:http://www.cnblogs.com/lee0oo0/archive/2012/08/23/2652751.html , Jackson可以轻松的将Java对象转换成json对象和xml ...

  6. VS2008非托管C++调用wcf(WebService)服务

    在Visual Studio 2008以及以后版本中,微软停止了非托管C++的直接WebService引用.不过ATL Server代码已经托管到开源网站上,我们可以找到ATL Server的源代码, ...

  7. drawInRect:withAttributes:

    - (void)drawRect:(CGRect)frame { NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defa ...

  8. bootstrap基础知识

    Bootstrap是Twitter推出的一款简洁.直观.强悍的前端开发框架. Bootstrap基于 HTML.CSS.JAVASCRIPT.它由Twitter的设计师Mark Otto和Jacob ...

  9. Qt下如何修改文件的时间(全平台修改)

    提供一个全平台修改文件的时间的方法,希望大家喜欢 /* UTIME.C: This program uses _utime to set the * file-modification time to ...

  10. 数据结构——AVL平衡树

    1.是二叉搜索树(Binary Search Tree) 2.树和所有左右子树高度之差为-1,0,1 平衡因子(balance factor) =右子树高度-左子树高度 平衡化旋转: 1.从插入位置向 ...