【LeetCode】No.104. Maximum Depth of Binary Tree -- Java Version

84 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第21天,点击查看活动详情.

题目链接:leetcode.com/problems/ma…

1. 题目介绍(Maximum Depth of Binary Tree)

Given the root of a binary tree, return its maximum depth.

【Translate】: 给定二叉树的根,返回其最大深度。

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

【Translate】: 二叉树的最大深度是从根节点到最远叶节点的最长路径上的节点数。

【测试用例】:

示例1:

testcase1

示例2:

testcase2

【条件约束】:

Constriants

2. 题解

凡是涉及到二叉树遍历相关,基本上有两种思想实现:①、深度优先搜索DFS;②、广度优先搜索BFS。

二叉树的深度优先遍历的非递归的通用做法是采用栈,广度优先遍历的非递归的通用做法是采用队列,具体可参考 yfcheng 的 Two Java Iterative solution DFS and BFS.

2.1 深度优先遍历(BFS)

深度优先遍历的思想是:从图中一个未访问的顶点 V 开始,沿着一条路一直走到底,然后从这条路尽头的节点回退到上一个节点,再从另一条路开始走到底...,不断递归重复此过程,直到所有的顶点都遍历完成,它的特点是不撞南墙不回头,先走完一条路,再换一条路继续走。

原题解来自于 hi-malik 的 [Java, C++] Easy to go Explanation & Solution. xmind 那么这道题的深度优先遍历路程就是,从根节点root出发,走1,走1.1,1.1为null,跳回上一节点,走1.2,1.2也为空,再跳回……依次类推,通过逐步的跳回,逐层累加,最后跳回到根节点,得到树的深度。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        else{
            int leftHeight = maxDepth(root.left);
            int rightHeight = maxDepth(root.right);
            return Math.max(leftHeight, rightHeight)+1;
        }
    }
}

act1

2.2 广度优先遍历(BFS)

广度优先遍历,指的是从图的一个未遍历的节点出发,先遍历这个节点的相邻节点,再依次遍历每个相邻节点的相邻节点,(层序顺序遍历),一般使用队列来实现,相关实现可参见 【LeetCode】No.102. Binary Tree Level Order Traversal -- Java Version. xmind2

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int ans = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
            ans++;
        }
        return ans;
    }
}

act2

3. 参考资料

[1] 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)| bilibili

[2] 104. 二叉树的最大深度 | CSDN

[3] 深度优先和广度优先比较 | 博客园