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

相关推荐
转调10 分钟前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
不穿格子衬衫38 分钟前
常用排序算法(下)
c语言·开发语言·数据结构·算法·排序算法·八大排序
wdxylb1 小时前
使用C++的OpenSSL 库实现 AES 加密和解密文件
开发语言·c++·算法
aqua35357423581 小时前
蓝桥杯-财务管理
java·c语言·数据结构·算法
CSP126361 小时前
特别节目————集训总结
c++
程序猿阿伟1 小时前
《C++游戏人工智能开发:开启智能游戏新纪元》
c++·人工智能·游戏
韬. .2 小时前
树和二叉树知识点大全及相关题目练习【数据结构】
数据结构·学习·算法
野草y2 小时前
数据结构(7.4_1)——B树
数据结构·b树
Word码2 小时前
数据结构:栈和队列
c语言·开发语言·数据结构·经验分享·笔记·算法
代码雕刻家2 小时前
数据结构-3.10.队列的应用
服务器·数据结构