/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};leetcode 104. Maximum Depth of Binary Tree
最新推荐文章于 2025-12-04 17:53:16 发布
本文介绍了一种计算二叉树最大深度的递归算法。通过遍历树的左右子节点,并递归地求取左右子树的最大深度,进而得出整棵树的最大深度。此算法简洁高效,易于理解和实现。
413

被折叠的 条评论
为什么被折叠?



