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 小时前
从零实现一个分布式日志平台:ELK的核心设计
c++
神仙别闹5 小时前
基于QT(C++)实现Windows 自启动项查看和分析
c++·windows·qt
WWTYYDS_6666 小时前
ProtoBuf超详细使用教程
c++
野生风长6 小时前
c++类和对象(this指针,重载operator,习题总结)
java·开发语言·c++
雪的季节6 小时前
Python「假多态」与 C++「真多态」的核心区别
开发语言·c++
zander2586 小时前
138. 随机链表的复制
数据结构·算法·链表
过期动态6 小时前
【LeetCode 热题 100】找到字符串中所有字母异位词
java·数据结构·算法·leetcode·职场和发展·rabbitmq
来一碗刘肉面7 小时前
栈在递归中的应用
数据结构·算法
zmzb01037 小时前
C++课后习题训练记录Day166
开发语言·c++
皓月斯语7 小时前
程序设计语言的特点
开发语言·数据结构·c++