java 二叉树的遍历(先序,中序,后序),递归+非递归

树的定义

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

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

先序遍历

递归

    public static void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.println(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }

非递归

public static List<Integer> preOrderWithWhile(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Deque<TreeNode> stack = new LinkedList<>();
        TreeNode tmp = root;
        while (tmp != null || !stack.isEmpty()) {

            // 遍历至叶子节点,并将遍历过程中的节点压入栈
            while (tmp != null) {
                stack.push(tmp);
                list.add(tmp.val); // 同时保存遍历过程中的节点
                tmp = tmp.left;
            }
            // 先序遍历 中 左 右
            if (!stack.isEmpty()) {
                // 将当前节点出栈,同时指针指向其右节点
                tmp = stack.pop();
                tmp = tmp.right; 
            }
        }
        return list;
    }

中序遍历

递归

public static void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.val);
        inOrder(root.right);
    }

非递归

public static List<Integer> inOrderWithWhile(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        TreeNode tmp = root;
        Deque<TreeNode> stack = new LinkedList<>();
        while (tmp != null || !stack.isEmpty()) {
            while (tmp != null) {
                stack.push(tmp);
                tmp = tmp.left;
            }

            if (!stack.isEmpty()) {
                tmp = stack.pop();
                list.add(tmp.val);
                tmp = tmp.right;
            }
        }
        return list;
    }

后序遍历

递归

public static void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.val);
    }

非递归

public static List<Integer> postOrderWithWhile(TreeNode root) {
        Deque<TreeNode> stack = new LinkedList<>();
        List<Integer> list = new ArrayList<>();
        TreeNode tmp = root;
        // 上一个遍历的节点
        TreeNode pre = null;

        while (tmp != null || !stack.isEmpty()) {
            while (tmp != null) {
                stack.push(tmp);
                tmp = tmp.left;
            }

            if (!stack.isEmpty()) {
                tmp = stack.peek();
               
                if (tmp.right == null || tmp.right == pre) {
                    stack.pop();
                    list.add(tmp.val);
                    pre = tmp;
                    tmp = null;
                } else {
                    tmp = tmp.right;
                }
            }
        }
        return list;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值