mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
34 lines
773 B
C++
34 lines
773 B
C++
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 {
|
|
private:
|
|
void maxDepth(TreeNode *root, int index, int *max) {
|
|
if (root == nullptr) {
|
|
if (index > *max) {
|
|
*max = index;
|
|
}
|
|
|
|
return;
|
|
} else {
|
|
maxDepth(root->left, index + 1, max);
|
|
maxDepth(root->right, index + 1, max);
|
|
}
|
|
}
|
|
|
|
public:
|
|
int maxDepth(TreeNode *root) {
|
|
int max = 0;
|
|
maxDepth(root, 0, &max);
|
|
|
|
return max;
|
|
}
|
|
};
|