-
Notifications
You must be signed in to change notification settings - Fork 3
/
GetMaximumDistance.cpp
86 lines (74 loc) · 2.22 KB
/
GetMaximumDistance.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;
/* 编程之美 原题
* http://www.cnblogs.com/miloyip/archive/2010/02/25/1673114.html
*/
// 参考另外一道题 http://www.lintcode.com/zh-cn/problem/binary-tree-maximum-path-sum/
struct NODE
{
NODE *pLeft;
NODE *pRight;
};
struct RESULT
{
int nMaxDistance;
int nMaxDepth;
};
RESULT GetMaximumDistance(NODE* root)
{
if (!root)
{
RESULT empty = { 0, -1 }; // trick: nMaxDepth is -1 and then caller will plus 1 to balance it as zero.
return empty;
}
RESULT lhs = GetMaximumDistance(root->pLeft);
RESULT rhs = GetMaximumDistance(root->pRight);
RESULT result;
result.nMaxDepth = max(lhs.nMaxDepth + 1, rhs.nMaxDepth + 1);
result.nMaxDistance = max(max(lhs.nMaxDistance, rhs.nMaxDistance), lhs.nMaxDepth + rhs.nMaxDepth + 2);
return result;
}
//////////Test code ////////
void Link(NODE* nodes, int parent, int left, int right)
{
if (left != -1)
nodes[parent].pLeft = &nodes[left];
if (right != -1)
nodes[parent].pRight = &nodes[right];
}
int main()
{
// P. 241 Graph 3-12
NODE test1[9] = { 0 };
Link(test1, 0, 1, 2);
Link(test1, 1, 3, 4);
Link(test1, 2, 5, 6);
Link(test1, 3, 7, -1);
Link(test1, 5, -1, 8);
cout << "test1: " << GetMaximumDistance(&test1[0]).nMaxDistance << endl;
// P. 242 Graph 3-13 left
NODE test2[4] = { 0 };
Link(test2, 0, 1, 2);
Link(test2, 1, 3, -1);
cout << "test2: " << GetMaximumDistance(&test2[0]).nMaxDistance << endl;
// P. 242 Graph 3-13 right
NODE test3[9] = { 0 };
Link(test3, 0, -1, 1);
Link(test3, 1, 2, 3);
Link(test3, 2, 4, -1);
Link(test3, 3, 5, 6);
Link(test3, 4, 7, -1);
Link(test3, 5, -1, 8);
cout << "test3: " << GetMaximumDistance(&test3[0]).nMaxDistance << endl;
// P. 242 Graph 3-14
// Same as Graph 3-2, not test
// P. 243 Graph 3-15
NODE test4[9] = { 0 };
Link(test4, 0, 1, 2);
Link(test4, 1, 3, 4);
Link(test4, 3, 5, 6);
Link(test4, 5, 7, -1);
Link(test4, 6, -1, 8);
cout << "test4: " << GetMaximumDistance(&test4[0]).nMaxDistance << endl;
return 0;
}