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

相关推荐
yolo_guo11 分钟前
opencv 学习: QA_02 什么是图像中的高频成分和低频成分
linux·c++·opencv·计算机视觉
AI科技星34 分钟前
引力编程时代:人类文明存续与升维
数据结构·人工智能·经验分享·算法·计算机视觉
2301_803554521 小时前
socket编程
c++
热爱编程的OP2 小时前
Linux进程池与管道通信详解:从原理到实现
linux·开发语言·c++
晚风吹长发9 小时前
二分查找算法+题目详解
c++·算法·二分查找
罗义凯10 小时前
其中包含了三种排序算法的注释版本(冒泡排序、选择排序、插入排序),但当前只实现了数组的输入和输出功能。
数据结构·c++·算法
kevien_G110 小时前
JAVA之二叉树
数据结构·算法
春蕾夏荷_72829772511 小时前
c++ easylogging 使用示例
c++·log·easylogging
syt_biancheng11 小时前
Day3算法训练(简写单词,dd爱框框,3-除2!)
开发语言·c++·算法·贪心算法
自然数e12 小时前
C++多线程【线程管控】之线程转移以及线程数量和ID
开发语言·c++·算法·多线程