二叉树前序、中序、后序、层序遍历的递归与非递归java实现
程序员文章站
2022-05-16 14:37:03
...
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;
public class TreeTraverse {
public static void main(String[] args){
//测试如下二叉树
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
Node root = new Node(1);
root.left = new Node(2);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right = new Node(3);
root.right.left = new Node(6);
root.right.right = new Node(7);
preOrderTraverse(root);
System.out.println();
preOrderTraverse2(root);
System.out.println();
inOrderTraverse(root);
System.out.println();
inOrderTraverse2(root);
System.out.println();
posOrderTraverse(root);
System.out.println();
posOrderTraverse2(root);
System.out.println();
floorTraverse(root);
}
//先序遍历的递归实现
public static void preOrderTraverse(Node node){
if(node == null)
return;
System.out.print(node.value + " ");
preOrderTraverse(node.left);
preOrderTraverse(node.right);
}
//先序遍历的非递归实现
public static void preOrderTraverse2(Node node){
if(node == null)
return;
Stack<Node> stack = new Stack<>();
stack.push(node);
while(!stack.isEmpty()){
node = stack.pop();
System.out.print(node.value + " ");
if(node.right != null)
stack.push(node.right);
if(node.left != null)
stack.push(node.left);
}
}
//中序遍历的递归实现
public static void inOrderTraverse(Node node){
if(node == null)
return;
inOrderTraverse(node.left);
System.out.print(node.value + " ");
inOrderTraverse(node.right);
}
//中序遍历的非递归实现
public static void inOrderTraverse2(Node node){
if(node == null)
return;
Stack<Node> stack = new Stack<>();
while(!stack.isEmpty() || node != null){
while(node != null){
stack.push(node);
node = node.left;
}
node = stack.pop();
System.out.print(node.value + " ");
node = node.right;
}
}
//后序遍历的递归实现
public static void posOrderTraverse(Node node){
if(node == null)
return;
posOrderTraverse(node.left);
posOrderTraverse(node.right);
System.out.print(node.value + " ");
}
//后序遍历的非递归实现
public static void posOrderTraverse2(Node node){
if(node == null)
return;
Stack<Node> stack = new Stack<>();
Stack<Node> help = new Stack<>();
stack.push(node);
while(!stack.isEmpty()){
node = stack.pop();
help.push(node);
if(node.left != null)
stack.push(node.left);
if(node.right != null)
stack.push(node.right);
}
while(!help.isEmpty())
System.out.print(help.pop().value + " ");
}
//层序遍历
public static void floorTraverse(Node node){
if(node == null)
return;
Queue<Node> queue = new LinkedBlockingQueue<>();
queue.add(node);
while(!queue.isEmpty()){
node = queue.poll();
System.out.print(node.value + " ");
if(node.left != null)
queue.add(node.left);
if(node.right != null)
queue.add(node.right);
}
}
}
class Node{
int value;
Node left;
Node right;
public Node(int value){
this.value = value;
this.left = null;
this.right = null;
}
}
推荐阅读
-
【算法】二叉树的前序、中序、后序、层序遍历和还原。
-
PHP基于非递归算法实现先序、中序及后序遍历二叉树操作示例
-
Python二叉树的遍历操作示例【前序遍历,中序遍历,后序遍历,层序遍历】
-
C二叉树前序遍历中序遍历后续遍历递归非递归
-
二叉树遍历 前序遍历 后序遍历 中序遍历 非递归前序遍历 二叉树遍历非递归
-
【LeetCode】二叉树各种遍历大汇总(秒杀前序、中序、后序、层序)递归 & 迭代
-
python实现二叉树的层序、前序、中序、后序、之字形遍历
-
左神算法:分别用递归和非递归方式实现二叉树先序、中序和后序遍历(Java版)
-
C++ 非递归实现二叉树的前中后序遍历
-
java二叉树的几种遍历递归与非递归实现代码