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;
    }
    };

相关推荐
Simple_core11 分钟前
cmake模板和问题总结
c++
2301_800256111 小时前
数据结构基础期末复习例题
数据结构·算法
劉宇雁2 小时前
C++ ASCII 3D无尽跑酷游戏
c++·游戏·3d
变量未定义~2 小时前
单调栈-四元组问题
数据结构·算法
杜子不疼.2 小时前
【C++ 在线五子棋对战】- 会话管理模块实现
开发语言·c++
有点。2 小时前
C++深度优先搜索(DFS)的概念(一)
开发语言·c++·深度优先
旖-旎2 小时前
《LeetCode 978 最长湍流子数组 || LeetCode 139 单词拆分》
c++·算法·leetcode·动态规划
aaPIXa6222 小时前
C++大型项目模块化拆分实战记录
开发语言·c++
石山代码2 小时前
C++23 新特性在 CLion 中的实战体验
开发语言·c++·c++23
在线OJ的阿川3 小时前
仿muduo库实现高并发服务器
linux·服务器·c++