前序 中序 后序 遍历 递归 非递归算法 java实现
前序遍历 非递归
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实现的更多相关文章
- 二叉树(前序,中序,后序,层序)遍历递归与循环的python实现
二叉树的遍历是在面试使比较常见的项目了.对于二叉树的前中后层序遍历,每种遍历都可以递归和循环两种实现方法,且每种遍历的递归实现都比循环实现要简洁.下面做一个小结. 一.中序遍历 前中后序三种遍历方法对 ...
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树: SDUT 1489 Description 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input 输入数据有多组,第一行是一个整数t (t& ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- 【C&数据结构】---关于链表结构的前序插入和后序插入
刷LeetCode题目,需要用到链表的知识,忽然发现自己对于链表的插入已经忘得差不多了,以前总觉得理解了记住了,但是发现真的好记性不如烂笔头,每一次得学习没有总结输出,基本等于没有学习.连复盘得机会都 ...
- 【11】-java递归和非递归二叉树前序中序后序遍历
二叉树的遍历 对于二叉树来讲最主要.最基本的运算是遍历. 遍历二叉树 是指以一定的次序访问二叉树中的每个结点.所谓 访问结点 是指对结点进行各种操作的简称.例如,查询结点数据域的内容,或输出它的值,或 ...
随机推荐
- JAVA GUI学习 - 总结
一:项目 二:重要组件补充 三:组件高级操作
- IOS文件沙盒
在进行IPhone开发的时候,常常需要将简单的数据保存到文件中,以便下次再重新打开的时候能够读取文件中保存的数据. 下面就来做一个简单的demo: 步骤: 1.创建一个SingleView项目,带有x ...
- Android JNI入门第三篇——jni头文件分析
一. 首先写了java文件: public class HeaderFile { private native void doVoid(); native int doShort(); native ...
- leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal
这道题是为数不多的感觉在读本科的时候见过的问题. 人工构造的过程是如何呢.兴许遍历最后一个节点一定是整棵树的根节点.从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右 ...
- Ext JS学习第十三天 Ext基础之 Ext.Element
•Ext.Element提供了181个方法,嗯,还没完,只是在4.1版本中是这样,最新的4.2版本貌似又增加了新方法,可谓是相当丰富给力.那么根据操作类型基本可以分为查询系.DOM操作系.样式操作系. ...
- 工具篇-TraceView
--- layout: post title: 工具篇-TraceView description: 让我们远离卡顿和黑屏 2015-10-09 category: blog --- ## 让我们远 ...
- [译]Stairway to Integration Services Level 12 - 高级日志配置
介绍 本文中,我们将结合之前学习的时间冒泡,日志记录,以及复制模型.建立一个自定义的SSIS包日志模型. SSIS Task事件回顾 Reviewing SSIS Task Events 在做实 ...
- 【Linux命令】查找命令
如果你想在当前目录下 查找"hello,world!"字符串,可以这样: grep -rn "hello,world!" *
- Iterator(迭代器)接口 --对象循环遍历
<?php class MyIterator implements Iterator { private $var = array(); public function __construct ...
- hive on spark的安装及问题
配置hive hive-site <property> <name>hive.metastore.uris</name> <value>thrift:/ ...