-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass07_problem05.cpp
85 lines (78 loc) · 1.57 KB
/
class07_problem05.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
#include<iostream>
#include<vector>
using namespace std;
class TreeNode
{
public:
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v)
{
val = v;
}
};
class PROBLEM05
{
public:
int get_depth(TreeNode* node){
int depth = 0;
while (node){
depth++ ;
node = node->left;
}
return depth;
}
//N的L次方
int pown(int N, int L){
int res = 1;
int temp = N;
while (L != 0){
if (L & 1 == 1){
res *= temp;
temp = temp * temp;
}
else
temp = temp * temp;
L = L >> 1;
}
return res;
}
int process(TreeNode* node, int depth){
int ans = 0;
if (node->left == nullptr && node->right==nullptr)
return 1;
//右数的最大深度到达最大深度
if (get_depth(node->right) == depth - 1){
node = node->right;
ans = pown(2, depth - 1) + process(node,depth-1);
}
//右树没有到达最大高度,那么这是右树的右树满树
else if (get_depth(node->right) < depth - 1){
int L = get_depth(node->right);
node = node->left;
ans = pown(2, L) + process(node, depth - 1);
}
return ans;
}
int get_res(TreeNode* node){
int d = get_depth(node);//最大深度,全文贯穿
cout << d << endl;
int ans = process(node, d);
return ans;
}
};
int main()
{
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->right->left = new TreeNode(6);
//root->right->right = new TreeNode(7);
PROBLEM05 P;
int ans = P.get_res(root);
cout << ans << endl;
return 0;
}