-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path104_MaximumDepthofBinaryTree.cpp
30 lines (27 loc) · 1.18 KB
/
104_MaximumDepthofBinaryTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "stdafx.h"
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
TreeNode(int x, TreeNode *_left, TreeNode *_right) : val(x), left(_left), right(_right) {}
};
//-----------------------------------------------------------------
#include <algorithm>
class Solution {
public:
int maxDepth(TreeNode *root) {
if (root == nullptr) return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
//-----------------------------------------------------------------
int main() {
Solution so;
cout << so.maxDepth(nullptr) << endl;
cout << so.maxDepth(new TreeNode(1)) << endl;
cout << so.maxDepth(new TreeNode(1, new TreeNode(2, new TreeNode(3), nullptr), nullptr)) << endl;
cout << so.maxDepth(new TreeNode(1, new TreeNode(2, new TreeNode(3), nullptr), new TreeNode(2))) << endl;
cout << so.maxDepth(new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)))) << endl;
cout << so.maxDepth(new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15, new TreeNode(1, new TreeNode(2), nullptr), nullptr), new TreeNode(7)))) << endl;
}