-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximum_Depth_of_Binary_Tree.cpp
More file actions
69 lines (58 loc) · 1.44 KB
/
Maximum_Depth_of_Binary_Tree.cpp
File metadata and controls
69 lines (58 loc) · 1.44 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Source : https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/
// Author : zheng yi xiong
// Date : 2014-12-25
/**********************************************************************************
*
* Given a binary tree, find its maximum depth.
* The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
*
**********************************************************************************/
#include "stdafx.h"
#include <string>
using namespace std;
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 (NULL == root)
{
return 0;
}
int leftDepth = 0, rightDepth = 0;
if (NULL != root->left)
{
leftDepth = maxDepth(root->left);
}
if (NULL != root->right)
{
rightDepth = maxDepth(root->right);
}
return leftDepth > rightDepth ? (leftDepth + 1) : (rightDepth + 1);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
TreeNode root(9);
TreeNode node2(6);
TreeNode node3(-3);
TreeNode node4(-6);
TreeNode node5(2);
TreeNode node6(2);
TreeNode node7(-6);
TreeNode node8(-6);
root.left = &node2;
root.right = &node3;
node3.left = &node4;
node3.right = &node5;
node5.left = &node6;
node6.left = &node7;
node6.right = &node8;
Solution solution;
int max_depth = solution.maxDepth(&root);
return 0;
}