二叉树前序、中序、后序、层序遍历详解


public class TreeNode<T> {

    public  TreeNode left;

    public  TreeNode right;

    public T val;

    public TreeNode(T val) {
        this.val = val;
    }

    public TreeNode() {
    }

    public static TreeNode get(){
        TreeNode<Integer> root =new TreeNode<>(5);
        root.left = new TreeNode<>(3);
        root.left.left = new TreeNode<>(2);
        root.left.right = new TreeNode<>(4);
        root.left.left.left = new TreeNode<>(1);
        root.left.left.left.left = new TreeNode<>(0);
        root.right = new TreeNode<>(7);
        root.right.right = new TreeNode<>(8);
        root.right.left = new TreeNode<>(6);
        return root;
    }
}
 //前序、中序遍历
 public static void preOrderNonRecursive(TreeNode<Integer> root){
	Stack<TreeNode<Integer>> stack = new Stack<>();
	while (true){
		while (root != null){
			stack.push(root);
			//前序输出
			//System.out.print(root.val+"\t");
			root = root.left;
		}
		if(stack.isEmpty()) break;
		root = stack.pop();
		System.out.print(root.val+"\t");//中序输出
		root = root.right;
	}
 }
前序遍历:前序先输出根节点,再输出左子节点,最后输出右子节点。                                                                                   输出结果: 5 3 2 1 0 4 7 6 8

中序遍历:中序先输出左子节点,在输出根节点,最后访问右节点                                                                                            输出结果:0 1 2 3 4 5 6 7 8

//前序、中序遍历步骤:

1)将根结点5放入栈中,取出左节点3作为根节点  栈中(5)
2)将根结点3放入栈中,取出左节点2作为根节点 栈中(5,3)
3)将根结点2放入栈中,取出左节点1作为根节点 栈中(5,3,2)
4)将根结点1放入栈中,取出左节点0作为根节点 栈中(5,3,2,1)
5)将根结点0放入栈中,取出左节点null作为根节点 栈中(5,3,2,1,0)
6)将0节点从栈中删除,取出右节点null做为根节点 栈中(5,3,2,1)
7)将1节点从栈中删除,取出右节点null做为根节点 栈中(5,3,2)
8)将2节点从栈中删除,取出右节点null做为根节点 栈中(5,3)
9)将3节点从栈中删除,取出右节点4做为根节点 栈中(5)
10)将根结点4放入栈中,取出左节点null作为根节点 栈中(5,4)
11)将4节点从栈中删除,取出右节点null做为根节点 栈中(5)
12)将5节点从栈中删除,取出右节点7做为根节点 栈中()
13)将根结点7放入栈中,取出左节点6作为根节点 栈中(7)
14)将根结点6放入栈中,取出左节点null作为根节点 栈中(7,6)
15)将6节点从栈中删除,取出右节点null做为根节点 栈中(7)
16)将7节点从栈中删除,取出右节点8做为根节点 栈中()
17)将根节点8放入栈中,取出左节点null作为根节点 栈中(8)
18)将8节点从栈中删除,取出右节点null做为根节点 栈中()
19)跳出循环


 //后序遍历
  public static void thePostOrderTraversal_Stack(TreeNode root) {  
        Stack<TreeNode> stack = new Stack<>();
        Stack<TreeNode> output = new Stack<>();//构造一个中间栈来存储逆后序遍历的结果
        TreeNode node = root;
        while (node != null || stack.size() > 0) {
            if (node != null) {
                output.push(node);
                stack.push(node);
                node = node.right;
            } else {
                node = stack.pop();
                node = node.left;
            }
        }
        System.out.println(output.size());
        while (output.size() > 0) {
            System.out.print(output.pop().val +"\t");
        }
    }

后序遍历:先输出左子节点,再输出右子节点,最后输出根节点  0 1 2 4 3 6 8 7 5

后序遍历步骤:
1)将根结点5放入栈中,取出右节点7作为根节点 output栈中(5)      栈中(5) 
2)将根节点7放入栈中,取出右节点8作为根节点 output栈中(5,7)       栈中(5,7)
3)将根节点8放入栈中,取出右节点null作为根节点放入栈中       output栈中(5,7,8)         栈中(5,7,8)
4)将8节点从栈中删除,将8左节点null作为根节点     栈中(5,7)
5)将7节点从栈中删除,将7左节点6作为根节点    栈中(5)
6)将根节点6放入栈中,取出右节点null作为根节点            output栈中(5,7,8,6)    栈中(5,6)
7)将6节点从栈中删除,将6做节点null做为根节点     栈中(5)
8)将5节点从栈中删除,将左节点3作为根节点            栈中()
9)将根节点3放入栈中,取出右节点4作为根节点  output栈中(5,7,8,6,3)    栈中(3)
10)将根节点4放入栈中,取出右节点null作为根节点          output栈中(5,7,8,6,3,4)       栈中(3,4)
11)将根节点4从栈中删除,取出左节点null作为根节点     栈中(3)
12)将根节点3从栈中删除,取出左节点2作为根节点      栈中()
13)将根节点2放入栈中,取出右节点null作为根节点 output栈中(5,7,8,6,3,4,2)     栈中(2)
14)将根节点2从栈中删除,取出左节点1作为根节点              栈中(1)
15)将根节点1放入栈中,取出右节点null作为根节点 output栈中(5,7,8,6,3,4,2,1)   栈中(1)
16)将根节点1从栈中删除,取出左节点0作为根节点       栈中(0)
15)将根节点0放入栈中,取出右节点null作为根节点 output栈中(5,7,8,6,3,4,2,1,0) 栈中(0)
16)将根节点0从栈中删除,取出左节点null作为根节点        栈中()

17)跳出循环

//层序遍历
	public static int treeDepth(TreeNode<Integer> root){
        if(root==null){
            return 0;
        }
        Queue<TreeNode<Integer>> queue = new LinkedList<>();
        queue.offer(root);
        int depth = 0;
        while (!queue.isEmpty()){
            int size = queue.size();
            TreeNode<Integer> cur;
            for(int i=0;i<size;i++){
                cur = queue.poll();
                System.out.print(cur.val +"\t");
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
            depth++;
        }
        return depth;
    }
//层序遍历:先访问树的第一层,再访问第二层节点....一直到访问最下面一层节点。在同一层节点中,以从左到右的顺序依次访问。   
   输出结果:5 3 7 2 4 6 8 1 0


层序遍历步骤:
   1)将5节点从栈中删除,将5节点的左节点3、右节点7放入队列(3,7)
   2)将3节点从队列删除,将3节点的左节点2、右节点4放入队列(7,2,4)
   3)将7节点从队列删除,将7节点的左节点6、右节点8放入队列(2,4,6,8)
   4)将2节点从队列删除,将2节点的左节点1放入队列(4,6,8,1)
   5)将4节点从队列删除(6,8,1)
   6)将6节点从队列删除(8,1)
   7)将8节点从队列删除(1)
   8)将1节点从队列删除,将1节点的左节点0放入队列(0)
   9)将0节点从队列删除()
   10)跳出循环

猜你喜欢

转载自blog.csdn.net/qq1010267837/article/details/79978121