104. Maximum Depth of Binary Tree

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

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.

Example 1:

复制代码
Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:

复制代码
Input: root = [1,null,2]
Output: 2

Constraints:

  • The number of nodes in the tree is in the range [0, 104].

  • -100 <= Node.val <= 100

    class Solution {
    public:
    int maxDepth(TreeNode* root) {
    if(root==nullptr)return 0;
    return max(maxDepth(root->left),maxDepth(root->right))+1;
    }
    };

相关推荐
闻缺陷则喜何志丹36 分钟前
【BFS】 P10864 [HBCPC2024] Genshin Impact Startup Forbidden II|普及+
c++·算法·宽度优先·洛谷
qianbo_insist1 小时前
c++ python 共享内存
开发语言·c++·python
手握风云-2 小时前
优选算法的链脉之韵:链表专题
数据结构·算法·链表
Coding小公仔2 小时前
LeetCode 151. 反转字符串中的单词
开发语言·c++·算法
稳兽龙2 小时前
P1098 [NOIP 2007 提高组] 字符串的展开
c++·算法·模拟
算法练习生2 小时前
Qt核心类QWidget及其派生类详解
开发语言·c++·qt
oioihoii3 小时前
C++11标准库算法:深入理解std::none_of
java·c++·算法
老虎06273 小时前
数据结构(Java)--位运算
java·开发语言·数据结构
小汉堡编程6 小时前
数据结构——vector数组c++(超详细)
数据结构·c++