68 字
1 分钟
LeetCode 面试经典150题 [64/150 二叉树的最大深度]
题目描述
给定一个二叉树 root ,返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
示例 1:

输入:root = [3,9,20,null,null,15,7]输出:3示例 2:
输入:root = [1,null,2]输出:2个人C++解答
递归
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */class Solution {public: int maxDepth(TreeNode* root) { if (root == nullptr) return 0; int l_depth = maxDepth(root->left); int r_depth = maxDepth(root->right); return max(l_depth,r_depth)+1; }}; 分享
如果这篇文章对你有帮助,欢迎分享给更多人!
LeetCode 面试经典150题 [64/150 二叉树的最大深度]
https://ericonline.cn/posts/leetcode-面试经典150题-64-150-二叉树的最大深度/ 部分信息可能已经过时
相关文章 智能推荐
1
LeetCode 面试经典150题 [75/150 二叉树的层平均值]
LeetCode 题解 2024-07-31
2
LeetCode 面试经典150题 [74/150 二叉树的右视图]
LeetCode 题解 2024-07-31
3
LeetCode 面试经典150题 [67/150 对称二叉树]
LeetCode 题解 2024-07-16
4
LeetCode 面试经典150题 [73/150 二叉树的最近公共祖先]
LeetCode 题解 2024-07-31
5
LeetCode 面试经典150题 [80/150 验证二叉搜索树]
LeetCode 题解 2024-07-31