二叉树之深度优先遍历与广度优先遍历

  • 通过二叉树的前序和中序遍历确定二叉树:就是找到中序遍历中二叉树的根结点的下标,然后根据左右两子树进行递归
  • 二叉树的深度优先遍历和广度优先遍历:就是利用栈和队列进行存取元素
/*
 * @Author:Beer
 * @Date:2019-10-06
 * @Description:
 * 通过二叉树的前序和中序遍历确定二叉树:就是找到中序遍历中二叉树的根结点的下标,然后根据左右两子树进行递归
 * 二叉树的深度优先遍历和广度优先遍历:就是利用栈和队列进行存取元素
 */

import java.util.*;

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

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

public class TestLeverTree {
    public static void main(String[] args) {
        int[] pre = {1, 2, 4, 7, 3, 5, 6, 8};
        int[] ino = {4, 7, 2, 1, 5, 3, 8, 6};
        //构造二叉树
        TreeNode root = buildTree(pre, 0, pre.length - 1, ino, 0, ino.length - 1);
        //进行二叉树的层次遍历
        levelTree(root);
        System.out.println();
        //非递归版本的深度优先遍历
        depthThree(root);
    }

    /**
     * 通过二叉树的前序、中序构造二叉树
     *
     * @param pre
     * @param ps
     * @param pe
     * @param ino
     * @param is
     * @param ie
     */
    public static TreeNode buildTree(int[] pre, int ps, int pe, int[] ino, int is, int ie) {
        if (ps > pe) {
            return null;
        }
        int val = pre[ps];
        int index = is;
        while (is < ie && ino[index] != val) {
            index++;
        }
        if (index > ie) {
            throw new RuntimeException("Illegal input");
        }
        TreeNode node = new TreeNode(val);
        // 递归构建当前根结点的左子树,左子树的元素个数:index-is+1个
        // 左子树对应的前序遍历的位置在[ps+1, ps+index-is]
        // 左子树对应的中序遍历的位置在[is, index-1]
        node.left = buildTree(pre, ps + 1, ps + index - is, ino, is, index - 1);
        // 递归构建当前根结点的右子树,右子树的元素个数:ie-index个
        // 右子树对应的前序遍历的位置在[ps+index-is+1, pe]
        // 右子树对应的中序遍历的位置在[index+1, ie]
        node.right = buildTree(pre, ps + index - is + 1, pe, ino, index + 1, ie);
        return node;
    }

    /**
     * 二叉树的层次遍历
     *
     * @param root
     */
    public static void levelTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            System.out.print(node.val);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }

        }
    }

    /**
     * 二叉树的深度优先遍历
     * @param root
     */
    public static void depthThree(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            System.out.print(node.val);
            if (node.right != null) {
                stack.add(node.right);
            }
            if (node.left != null) {
                stack.add(node.left);
            }
        }
    }
}

结果:
12345678
12473568

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值