二叉树l结构类定义:
测试二叉树:
1
2 3
4 5 6 7
8 10
9
先序遍历
输出:
递归先序遍历:1 2 4 8 9 10 5 3 6 7
非递归先序遍历:1 2 4 8 9 10 5 3 6 7
中序遍历:
输出:
递归中序遍历:8 9 4 10 2 5 1 6 3 7
非递归中序遍历:8 9 4 10 2 5 1 6 3 7
后序遍历:
输出:
递归后序遍历:9 8 10 4 5 2 6 7 3 1
非递归后序遍历:9 8 10 4 5 2 6 7 3 1
public class TreeNode {
int val ;
TreeNode left ;
TreeNode right ;
public TreeNode(int val){
this.val = val ;
left = null ;
right = null ;
}
}
测试二叉树:
1
2 3
4 5 6 7
8 10
9
先序遍历
public class PreOrder {
//递归方法
public static void preorder1(TreeNode root){
if(root == null) return ;
System.out.print(root.val + " ");
if(root.left!=null) preorder1(root.left);
if(root.right != null) preorder1(root.right);
}
//非递归方法
public static void preorder2(TreeNode root){
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
if(root == null) return ;
TreeNode p = root ;
stack.push(p);
while(!stack.isEmpty()){
TreeNode temp = stack.pop();
System.out.print(temp.val + " ");
if(temp.right != null) stack.push(temp.right);
if(temp.left != null) stack.push(temp.left);
}
}
public static void main(String[] args){
TreeNode root = GeneratorTree.getTree();
System.out.print("递归先序遍历:");
preorder1(root);
System.out.println();
System.out.print("非递归先序遍历:");
preorder2(root);
}
}
输出:
递归先序遍历:1 2 4 8 9 10 5 3 6 7
非递归先序遍历:1 2 4 8 9 10 5 3 6 7
中序遍历:
public class InOrder {
//递归遍历
public static void inorder1(TreeNode root){
if(root == null) return ;
if(root.left!=null) inorder1(root.left);
System.out.print(root.val + " ");
if(root.right != null) inorder1(root.right);
}
//非递归遍历
public static void inorder2(TreeNode root){
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
if(root == null ) return ;
TreeNode p = root ; ;
while(p != null || !stack.isEmpty()){
if(p != null){
stack.push(p);
p = p.left;
}
else{
p = stack.pop();
System.out.print(p.val + " ");
p = p.right ;
}
}
}
public static void main(String[] args){
TreeNode root = GeneratorTree.getTree();
System.out.print("递归中序遍历:");
inorder1(root);
System.out.println();
System.out.print("非递归中序遍历:");
inorder2(root);
}
}
输出:
递归中序遍历:8 9 4 10 2 5 1 6 3 7
非递归中序遍历:8 9 4 10 2 5 1 6 3 7
后序遍历:
public class PostOrder {
//递归遍历
public static void postorder1(TreeNode root){
if(root == null) return ;
if(root.left!=null) postorder1(root.left);
if(root.right != null) postorder1(root.right);
System.out.print(root.val + " ");
}
//非递归遍历
public static void postorder2(TreeNode root){
if(root == null) return ;
TreeNode p = root ;
TreeNode pre = p ; //记录最近出栈元素
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
boolean flag = true ; //标记变量,表示当前是继续入栈还是出栈
while( p!= null || !stack.isEmpty()){
if(p!=null && flag == true){
stack.push(p);
p = p.left ;
}
else{
if(stack.isEmpty()) return ;
p = stack.peek() ;
if(p.right != null && p.right != pre){
p = p.right ;
flag = true ;
}
else{
p = stack.pop();
System.out.print(p.val + " ");
pre = p ;
flag = false ;
}
}
}
}
public static void main(String[] args){
TreeNode root = GeneratorTree.getTree();
System.out.print("递归后序遍历:");
postorder1(root);
System.out.println();
System.out.print("非递归后序遍历:");
postorder2(root);
}
}
输出:
递归后序遍历:9 8 10 4 5 2 6 7 3 1
非递归后序遍历:9 8 10 4 5 2 6 7 3 1