forked from jyx-fyh/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxRoute.cpp
More file actions
54 lines (49 loc) · 1.3 KB
/
maxRoute.cpp
File metadata and controls
54 lines (49 loc) · 1.3 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
//
// Created by ButcherX on 23-10-27.
//
#include"../header/treenode.h"
//个人解法
struct info
{
int maxHeight;
int maxDis;
info(int height, int dis):maxHeight(height), maxDis(dis){}
};
info findMaxDis_in(TreeNode* nd)
{
if(nd == nullptr)
{
info inf(0, 0);
return inf;
}
info infl = findMaxDis_in(nd->left);
info infr = findMaxDis_in(nd->right);
info infc(0, 0);
infc.maxDis = std::max(std::max(infl.maxDis, infr.maxDis), infl.maxHeight + infr.maxHeight);
infc.maxHeight = std::max(infl.maxHeight, infr.maxHeight) + 1;
return infc;
}
int findMaxDis(TreeNode* root)
{
info inf = findMaxDis_in(root);
return std::max(inf.maxHeight - 1, inf.maxDis);
}
//官方解法
class Solution{
int ans;
int depth(TreeNode* rt){
if (rt == nullptr) {
return 0; // 访问到空节点了,返回0
}
int L = depth(rt->left); // 左儿子为根的子树的深度
int R = depth(rt->right); // 右儿子为根的子树的深度
ans = std::max(ans, L + R + 1); // 计算d_node即L+R+1 并更新ans
return std::max(L, R) + 1; // 返回该节点为根的子树的深度
}
public:
int diameterOfBinaryTree(TreeNode* root) {
ans = 1;
depth(root);
return ans - 1;
}
};