#include <iostream>
#include <vector>
using namespace std;
typedef struct treeNode {
int value;
struct treeNode* left;
struct treeNode* right;
treeNode(int x) : value(x), left(nullptr), right(nullptr) {}
} Node;
int maxDepth(Node* root)
{
if(root == nullptr)
{
return 0;
}
else
{
int leftdepth = maxDepth(root->left);
int rightdepth = maxDepth(root->right);
return (leftdepth > rightdepth ? leftdepth : rightdepth) + 1;
}
}
int main()
{
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
cout << "depth : " << maxDepth(root) << endl;
system("pause");
return 0;
}
// 1
// 2 3
// 4 5
这个深度是3