二叉树的三种遍历(递归和非递归、模板)

递归实现:

import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int val) {
        this.val = val;
    }
}

public class Solution {

    private TreeNode createBinaryTree() {
        TreeNode root = new TreeNode(0);

        root.left = new TreeNode(1);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.right = new TreeNode(2);
        root.right.right = new TreeNode(5);

        return root;
    }

    public static void main(String[] args) {
        Solution test = new Solution();

        TreeNode root = test.createBinaryTree();

        test.preOrder(root);
        System.out.println("\n=============================================");
        test.midOrder(root);
        System.out.println("\n=============================================");
        test.postOrder(root);
        System.out.println("\n=============================================");
        test.levelOrder(root);
        System.out.println("\n=============================================");

    }

    // 前序遍历
    private void preOrder(TreeNode root) {
        if(root == null) return;

        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    private void midOrder(TreeNode root) {
        if(root == null) return;

        midOrder(root.left);
        System.out.print(root.val + " ");
        midOrder(root.right);
    }

    // 后序遍历
    private void postOrder(TreeNode root) {
        if(root == null) return;

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    // 层序遍历
    private void levelOrder(TreeNode root) {
        if(root == null) return;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode t = queue.poll();
            if(t.left != null) queue.offer(t.left);
            if(t.right != null) queue.offer(t.right);

            System.out.print(t.val + " ");
        }
    }

}

输出: 

非递归实现:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int val) {
        this.val = val;
    }
}

public class Solution {

    private TreeNode createBinaryTree() {
        TreeNode root = new TreeNode(0);

        root.left = new TreeNode(1);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.right = new TreeNode(2);
        root.right.right = new TreeNode(5);

        return root;
    }

    public static void main(String[] args) {
        Solution test = new Solution();

        TreeNode root = test.createBinaryTree();

        test.preOrder(root);
        System.out.println("\n=============================================");
        test.midOrder(root);
        System.out.println("\n=============================================");
        test.postOrder(root);
        System.out.println("\n=============================================");
        test.levelOrder(root);
        System.out.println("\n=============================================");

    }

    // 前序遍历
    private void preOrder(TreeNode root) {

        Stack<TreeNode> stack = new Stack<>();
        while(!stack.isEmpty() || root != null) {
            while(root != null) {
                stack.push(root);
                System.out.print(root.val + " ");
                root = root.left;
            }

            if(!stack.isEmpty()) {
                root = stack.pop();
                root = root.right;
            }
        }
    }

    // 中序遍历
    private void midOrder(TreeNode root) {

        Stack<TreeNode> stack = new Stack<>();
        while(!stack.isEmpty() || root != null) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }

            if(!stack.isEmpty()) {
                root = stack.pop();
                System.out.print(root.val + " ");
                root = root.right;
            }
        }
    }

    // 后序遍历
    private void postOrder(TreeNode root) {

        Stack<TreeNode> stack = new Stack<>();
        Stack<Integer> ans = new Stack<>();
        while(!stack.isEmpty() || root != null) {
            while(root != null) {
                stack.push(root);
                ans.push(root.val);
                root = root.right;
            }

            if(!stack.isEmpty()) {
                root = stack.pop();
                root = root.left;
            }
        }
        
        while(!ans.isEmpty()) {
            System.out.print(ans.pop() + " ");
        }
    }

    // 层序遍历
    private void levelOrder(TreeNode root) {
        if(root == null) return;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode t = queue.poll();
            if(t.left != null) queue.offer(t.left);
            if(t.right != null) queue.offer(t.right);

            System.out.print(t.val + " ");
        }
    }

}

输出:

能力有限,未写推理过程,觉得这篇文章写的很不错,有兴趣的朋友可以去了解下:https://blog.csdn.net/zhangxiangdavaid/article/details/37115355 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值