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

相关推荐
艾莉丝努力练剑2 小时前
【AI接入大模型SDK】Markdown格式解析
前端·c++·人工智能·学习·ai·架构
逐光沧海2 小时前
数据结构-链表习题(C++)
数据结构·c++·链表
是main不是漫12 小时前
【快慢指针 | LeetCode202 快乐数】鸽巢原理佐证环特性,快慢指针巧解快乐数
c语言·开发语言·c++·算法
Sashtok16 小时前
cpp基础知识(10)
c++·后端
whitelbwwww17 小时前
CUDA C++实战--中级
开发语言·前端·c++
老师我太想进步了202619 小时前
【数据结构-12】树与二叉树的结构、概念
数据结构
ShineWinsu20 小时前
对于Qt:QMessageBox的解析
开发语言·c++·qt·面试·客户端·qmessagebox·消息对话框
小灰灰搞电子1 天前
C++ initializer_list 详解
c++·list
2601_967980111 天前
Python中heapq模块的各种用法实例
数据结构·python·算法·应用实例·heapq模块
zaizai10071 天前
算法(左程云)2
数据结构