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

相关推荐
汉克老师5 分钟前
GESP5级C++考试语法知识(八、链表(三)循环链表)
c++·约瑟夫问题·循环链表·gesp5级·gesp五级
月落归舟8 分钟前
帮你从算法的角度来认识数组------( 二 )
数据结构·算法·数组
阿贵---24 分钟前
C++中的RAII技术深入
开发语言·c++·算法
PiKaMouse.1 小时前
navigation2-humble从零带读笔记第一篇:nav2_core
c++·算法·机器人
lightqjx1 小时前
【算法】二分算法
c++·算法·leetcode·二分算法·二分模板
Irissgwe2 小时前
进程间通信
linux·服务器·网络·c++·进程间通信
Wave8452 小时前
数据结构—树
数据结构
ic爱吃蓝莓2 小时前
数据结构 | HashMap原理
数据结构·学习·算法·链表·哈希算法
add45a2 小时前
C++编译期数据结构
开发语言·c++·算法
灰色小旋风2 小时前
力扣21 合并两个有序链表(C++)
c++·leetcode·链表