LeetCode(104):二叉树的最大深度

Easy!

题目描述:

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7]

    3
   /   9  20
    /     15   7

返回它的最大深度 3 。

解题思路:

求二叉树的最大深度问题用到深度优先搜索DFS递归的完美应用,跟求二叉树的最小深度问题原理相同。

C++解法一:

1 class Solution {
2 public:
3     int maxDepth(TreeNode* root) {
4         if (!root) return 0;
5         return 1 + max(maxDepth(root->left), maxDepth(root->right));
6     }
7 };

我们也可以使用层序遍历二叉树,然后计数总层数,即为二叉树的最大深度。

C++解法二:

 1 class Solution {
 2 public:
 3     int maxDepth(TreeNode* root) {
 4         if (!root) return 0;
 5         int res = 0;
 6         queue<TreeNode*> q;
 7         q.push(root);
 8         while (!q.empty()) {
 9             ++res;
10             int n = q.size();
11             for (int i = 0; i < n; ++i) {
12                 TreeNode *t = q.front(); q.pop();
13                 if (t->left) q.push(t->left);
14                 if (t->right) q.push(t->right);
15             }
16         }
17         return res;
18     }
19 };

 

原文地址:https://www.cnblogs.com/ariel-dreamland/p/9162411.html

时间: 2024-08-20 07:04:02

LeetCode(104):二叉树的最大深度的相关文章

图解精选 TOP 面试题 002 | LeetCode 104. 二叉树的最大深度

该系列题目取自 LeetCode 精选 TOP 面试题列表:https://leetcode-cn.com/problemset/top/ 题目描述 原题链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明:叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20

Leetcode 104. 二叉树的最大深度

题目链接 https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/ 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 返回它的最大深度 3 . 题解 深度遍历,使用递归的解法. 代码 /** * Definitio

Leetcode题目104.二叉树的最大深度(DFS+BFS简单)

题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 返回它的最大深度 3 . 思路分析:递归(二叉树最大深度,等于左右子树的最大深度+1) 代码实现: 一.深度优先比遍历(DFS) /** * Definition for a binary tree node. * public class TreeNo

104. 二叉树的最大深度

给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 返回它的最大深度 3 . 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right;

124. 二叉树中的最大路径和/104. 二叉树的最大深度

我最想刷的递归又来啦,待我先去好好研究一下. 据说T124是T104的进阶,所以先去刷下104 104: 顶层: 即在真正的root节点上,我要返回的是一个数字,而且要比我的左右节点返回的深度大1 def maxDepth(self,root): return max(maxDepth(root.left)+1,maxDepth(root.right)+1) 底层:当遇到叶子节点时候,深度为1,其左右的None 深度为0,所以basecase 应该是叶子节点的再下一层,不要搞错了. if not

Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 分析:求二叉树的最大深度 解法一:很容易想到的便是递归(深度优先搜索) (1)如果根节点是空,则返回0:否则转到(2) (2)  l = 左子树的最大深度; r = 右子树的最大深

leetcode.104 计算二叉树的最大深度

题目描述:给一个二叉树,返回该二叉树的最大深度 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A leaf is a node with no children. Example: Given binary tre

[LeetCode系列] 二叉树最大深度求解问题(C++递归解法)

问: 给定二叉树, 如何计算二叉树最大深度? 算法描述如下: 如果当前节点为空, 返回0(代表此节点下方最大节点数为0) 如果当前节点不为空, 返回(其左子树和右子树下方最大节点数中的最大值+1) 上述算法的精髓在于递归调用中的终止条件. 代码如下: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNod

97.二叉树的最大深度

转自[LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度 /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Soluti