二叉树的前\中\后\层序遍历

前序遍历

//先序遍历
//递归方法
    public static void prevOrder(Node root) {
        if (root == null) {
            return;//空树不需要遍历
        }
        //先访问根节点
        //再递归左子树
        //再递归右子树
        System.out.print(root.val);
        prevOrder(root.left);
        prevOrder(root.right);
    }

 //非递归前序,迭代法,使用栈
    public static void prevOrder2(Node root) {
        Stack<Node> stack = new Stack<>();
        Node cur = root;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val);
                cur = cur.left;
            }
            cur = stack.pop();
            cur = cur.right;
        }
    }

中序遍历

//中序遍历
//递归法
    public static void inOrder(Node root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);//先遍历左子树
        System.out.print(root.val);//访问根节点
        inOrder(root.right);//再遍历右子树
    }

    //非递归中序,迭代法
    public static void inOrder2(Node root) {
        Stack<Node> stack = new Stack<>();
        Node cur = root;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);//入栈
                cur = cur.left;//继续向左
            }
            cur = stack.pop();//为null出此时栈顶元素
            System.out.print(cur.val);//并打印
            cur = cur.right;//指向右边
        }
    }
//迭代法,规定返回值为List<Character>
    public static List<Character> inOrder3(Node root) {
        List<Character> list = new ArrayList<>();
        Stack<Node> stack = new Stack<>();
        Node cur = root;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            list.add(cur.val);
            cur = cur.right;
        }
        return list;
    }

  

后序遍历

  //后序遍历
  //递归方法
    public static void postOrder(Node root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val);
    }

    //非递归后序遍历,迭代法
    public static void postOrder1(Node root) {
        Stack<Node> stack = new Stack<>();
        Node cur = root;
        Node flg = null;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.peek();
            if (cur.right == null || cur.right == flg) {
                System.out.print(cur.val);
                stack.pop();
                flg = cur;//标记打印的前一个值
                cur = null;
            } else {
                cur = cur.right;
            }
        }
    }
//迭代法,使用栈和集合
    public static List<Character> postOrder2(Node root) {
        List<Character> list = new ArrayList<>();
        Stack<Node> stack = new Stack<>();
        Node cur = root;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                list.add(cur.val);
                stack.push(cur.left);
                cur = cur.right;
            }
            cur = stack.peek();
            stack.pop();
        }
        Collections.reverse(list);//逆置输出结果
        return list;
    }

层序遍历

//层序遍历
    public static void levelOrderTraversal(Node root) {
        Queue<Node> queue = new LinkedList<>();//使用队列
        if (root != null) {
            queue.offer(root);//先入根节点
        }
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            if (cur == null) {
                return;
            }
            System.out.print(cur.val);
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值