Java实现-二叉树先序,中序,后序遍历及递归,非递归遍历
来源:互联网 发布:台州网络答题知识竞赛 编辑:程序博客网 时间:2024/04/30 23:08
对于二叉树的遍历一共存在两种类型:递归遍历和非递归遍历,针对每种类型有分为先序遍历,中序遍历和后序遍历。以下是六种遍历执行过程。
//先序遍历二叉树(递归形式)
public List<Integer> preOrder1(TreeNode root){if( root != null){
reslist.add(root.val);
System.out.print(root.val+";");
preOrder1(root.leftchild);
preOrder1(root.rightchild);
}
return reslist;
}
//先序遍历二叉树(非递归形式)
public List<Integer> preOrder2(TreeNode root){
Stack<TreeNode> nodestack = new Stack<TreeNode>();
TreeNode node = root;
while(root != null || !nodestack.isEmpty()){
while( root != null ){
//先将根结点添加到顺序表中,然后是打印输出
reslist.add(node.val);
System.out.println(node.val+";");
nodestack.push(node);
node = node.leftchild;
}
//出栈顺序是:先是根结点,其次是左子树结点,最后是右子树结点
node = nodestack.pop();
node = node.rightchild;
}
return reslist;
}
//中序遍历二叉树(递归形式)
public List<Integer> InOrder1(TreeNode root){
if(root != null){
InOrder1(root.leftchild);
reslist.add(root.val);
System.out.print(root.val+";");
InOrder1(root.rightchild);
}
return reslist;
}
//中序遍历二叉树(非递归形式)
public List<Integer> InOrder2(TreeNode root){
Stack<TreeNode> nodestack = new Stack<TreeNode>();
TreeNode node = root;
while( node != null || !nodestack.isEmpty()){
//将结点存入栈中
while( node != null){
nodestack.push(node);
node = node.leftchild;
}
//出栈顺序先是最左端的结点 出栈,接着是根结点出栈,最后是右子树结点
node = nodestack.pop();
//将出栈结点添加到顺序表中,并打印输出
reslist.add(node.val);
System.out.print(node.val+";");
node = node.rightchild;
}
return reslist;
}
//后序遍历二叉树(递归形式)
public List<Integer> PostOrder1(TreeNode root){
if(root != null){
PostOrder1(root.leftchild);
PostOrder1(root.rightchild);
reslist.add(root.val);
System.out.println(root.val+";");
}
return reslist;
}
//后序遍历二叉树(非递归形式)
public List<Integer> PostOrder2(TreeNode root){
Stack<TreeNode> nodestack = new Stack<TreeNode>();
List<Integer> nodelist = new ArrayList<Integer>();
TreeNode node = root;
while( node != null || !nodestack.isEmpty()){
while( node != null || !nodelist.contains(node)){
nodestack.push(node);
node = node.leftchild;
}
if( !nodestack.isEmpty()){
node = nodestack.pop();
reslist.add(node.val);
System.out.print(node.val+";");
if( !nodestack.isEmpty()){
node = nodestack.peek();
node = node.rightchild;
}
}
}
return reslist;
}
说明:对于上述出现的reslist顺序表的定义在上一篇博客中以给出。点击打开链接
0 0
- Java实现-二叉树先序,中序,后序遍历及递归,非递归遍历
- 二叉树先序,中序,后序遍历非递归实现
- 二叉树先序,中序,后序遍历非递归实现
- 二叉树先序,中序,后序遍历非递归实现
- 二叉树先序,中序,后序遍历非递归实现
- 二叉树先序,中序,后序遍历非递归实现
- 二叉树先序,中序,后序遍历非递归实现
- 二叉树先序,中序,后序遍历非递归实现
- 二叉树先序遍历,中序遍历和后序遍历的非递归实现
- 二叉树的前序、中序、后序(递归、非递归)遍历java实现
- JAVA实现二叉树的前、中、后序遍历(递归与非递归)
- 二叉树先序遍历,中序遍历,后序遍历递归非递归方法
- 算法:二叉树的先序遍历、中序遍历、后序遍历(递归及非递归方式)的java代码实现
- 二叉树非递归前、中、后序遍历实现
- 二叉树先序,中序,后序的非递归遍历(附java实现)
- 分别用递归和非递归方式实现二叉树先序、中序和后序遍历(java实现)
- 二叉树先序,中序,后序、层序遍历递归和非递归实现
- 数据结构 分别用递归和非递归方法实现二叉树先序,中序,后序遍历
- Qt中出错error: undefined reference to `WSAStartup@8'
- hdu 1028Ignatius and the Princess III(dp)
- 第九周 项目1-深复刻体验
- 第五周项目二(1)游戏中的角色类
- e2fsck -f -y -v -C 0 /dev/sda9
- Java实现-二叉树先序,中序,后序遍历及递归,非递归遍历
- DNS负载均衡问题
- hdu 1398Square Coins(dp或者母函数)
- 游戏中的角色类增强版
- 数组类运算的实现
- 薪酬、技术和未来——首份程序员技术增值报告
- C++学习笔记(六)可变形参函数
- Best packages for data manipulation in R
- C++实验6-矩阵求和