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

相关推荐
雾岛听蓝13 分钟前
红黑树深度解析:设计原理与实现逻辑
c++
gjxDaniel18 分钟前
A+B问题天堂版
c++·算法·字符串·字符数组
M__3322 分钟前
动态规划进阶:简单多状态模型
c++·算法·动态规划
米优29 分钟前
使用Qt实现消息队列中间件动态库封装
c++·中间件·rabbitmq
N.D.A.K33 分钟前
CF2138C-Maple and Tree Beauty
c++·算法
AI视觉网奇41 分钟前
ue 5.5 c++ mqtt 订阅/发布 json
网络·c++·json
程序员-King.1 小时前
day159—动态规划—打家劫舍(LeetCode-198)
c++·算法·leetcode·深度优先·回溯·递归
txinyu的博客1 小时前
解析muduo源码之 StringPiece.h
开发语言·网络·c++
浅念-1 小时前
C语言——单链表
c语言·开发语言·数据结构·经验分享·笔记·算法·leetcode
夏乌_Wx1 小时前
练题100天——DAY40:合并两个有序链表
数据结构