✿-23例题 递归、广度优先搜索--二叉树的最大深度-104

Posted on 2020-11-18 13:14  MissRong  阅读(126)  评论(0)    收藏  举报

✿-23例题 递归、广度优先搜索--二叉树的最大深度-104

一、题目

二、Java实现代码

1.递归实现

package ShenDuYouXianSouSuo;

import lombok.AllArgsConstructor;

/**
 * @Author : ASUS and xinrong
 * @Version : 2020/11/18 & 1.0
 * 递归、广度优先遍历
 * 二叉树的最大深度-104
 * 思路:遍历根节点的左右子树的深度并选择最大的,最后+1
 */
public class MaximumDepthOfBinaryTree_Recursion {
    @AllArgsConstructor
    static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }

    /**
     * 递归
     * @param root
     * @return
     * 时间复杂度:O(n) n-二叉树的节点数,每个节点只被遍历到一次
     * 空间复杂度度:O(n) n-树的深度
     */
    public int maxDepth(TreeNode root) {
        //特判
        if (root == null) {
            return 0;
        }
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        return Math.max(leftDepth, rightDepth) + 1;
//        注:该方法的实现可以直接简化成一行代码【国际站上最高票解】
//        return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(3);
        treeNode.left = new TreeNode(9);
        treeNode.right = new TreeNode(20, new TreeNode(15), new TreeNode(7));
        MaximumDepthOfBinaryTree_Recursion maximumDepthOfBinaryTree = new MaximumDepthOfBinaryTree_Recursion();
        System.out.println(maximumDepthOfBinaryTree.maxDepth(treeNode));
    }
}

2.BFS-广度优先遍历实现

package ShenDuYouXianSouSuo;

import lombok.AllArgsConstructor;

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

/**
 * @Author : ASUS and xinrong
 * @Version : 2020/11/18 & 1.0
 * 递归、广度优先遍历
 * 二叉树的最大深度-104
 */
public class MaximumDepthOfBinaryTree_BFS {
    @AllArgsConstructor
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }

    /**
     * 广度优先遍历
     * BFS
     * 队列中只保存当前层的所有节点 (时间复杂度:O(n)--n代表树的节点数,空间复杂度:O(n)--n代表队列中存储的元素的个数)
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {
        int depth = 0;
        //特判
        if (root == null) {
            return depth;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                TreeNode pollNode = queue.poll();
                if (pollNode.left != null) {
                    queue.add(pollNode.left);
                }
                if (pollNode.right != null) {
                    queue.add(pollNode.right);
                }
            }
            depth++;
        }
        return depth;
    }
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(3);
        treeNode.left = new TreeNode(9);
        treeNode.right = new TreeNode(20, new TreeNode(15), new TreeNode(7));
        MaximumDepthOfBinaryTree_BFS maximumDepthOfBinaryTree = new MaximumDepthOfBinaryTree_BFS();
        System.out.println(maximumDepthOfBinaryTree.maxDepth(treeNode));
    }
}

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3