二叉树相关算法

简单粗暴,直接上代码

层序遍历

    public static ArrayList<Integer> printFromTopToBottom(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();  //存放结果
        Queue<TreeNode> queue = new LinkedList<>();   //辅助队列
        if (root != null) {
            //根节点入队
            queue.offer(root);
        }
        //队列不为空,执行循环
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            list.add(node.data);     //将队列元素输出

            //如果有左节点,就把左节点加入
            if (node.left != null) {
                queue.offer(node.left);
            }
            //如果有右节点,就把右节点加入
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
        list.forEach(System.out::print);
        return list;
    }

求最大宽度(包含值为null的节点)

*假设满二叉树表示成数组序列, 根节点所在的位置为1, 则任意位于i节点的左右子节点的index为2*i, 2*i+1,用一个List保存每层的左端点, 易知二叉树有多少层List的元素就有多少个. 那么可以在dfs的过程中记录每个节点的index及其所在的层level, 如果level > List.size()说明当前节点就是新的一层的最左节点, 将其加入List中, 否则判断当前节点的index减去List中对应层的最左节点的index的宽度是否大于最大宽度并更新。

    private int maxW = 0;

    public int widthOfBinaryTree(TreeNode root) {
        dfs(root, 1, 1, new ArrayList<Integer>());
        return maxW;
    }

    private void dfs(TreeNode r, int level, int index, ArrayList<Integer> left) {

        if (r == null) {
            return;
        }
        if (level > left.size()) {
            left.add(index);
        }
        maxW = Math.max(maxW, index - left.get(level - 1) + 1);

        dfs(r.left, level + 1, index * 2, left);
        dfs(r.right, level + 1, index * 2 + 1, left);
    }

求最大深度

    public static int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            int left_height = maxDepth(root.left);
            int right_height = maxDepth(root.right);
            return Math.max(left_height, right_height) + 1;
        }
    }

翻转二叉树

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode tmp = root.right;
        root.right = root.left;
        root.left = tmp;
        //翻转下一次层级,判断非空
        if (root.right != null) {
            invertTree(root.right);
        }
        //翻转下一次层级,判断非空
        if (root.left != null) {
            invertTree(root.left);
        }
        return root;
    }

前/中/后序遍历

    /**
     * 前序遍历
     */
    static void preOrder(TreeNode root) {
        if (root == null) return;
        System.out.print(root.data);
        preOrder(root.left);
        preOrder(root.right);
    }

    /**
     * 中序遍历
     */
    static void inOrder(TreeNode root) {
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.data);
        inOrder(root.right);
    }

    /**
     * 后序遍历
     */
    static void postOrder(TreeNode root) {
        if (root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.data);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值