二叉树最简单的性质之一,递归可得。
时间复杂度:O(N)
C++代码:
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr)
return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
二叉树最简单的性质之一,递归可得。
时间复杂度:O(N)
C++代码:
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr)
return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};