-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
77 lines (62 loc) · 1.74 KB
/
main.cc
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000010;
const int inf = 1e8;
const int mod = 1000000007;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int dfs(TreeNode* root, bool is_root, int& require) {
if (root->left == nullptr && root->right == nullptr) {
require = 1;
return 0;
} else if (root->left == nullptr || root->right == nullptr) {
if (root->left == nullptr) {
root->left = root->right;
root->right = nullptr;
}
int ans = dfs(root->left, false, require);
if (is_root && require) {
ans += 1;
}
return ans;
} else {
int require_left = 0;
int require_right = 0;
int left = dfs(root->left, false, require_left);
int right = dfs(root->right, false, require_right);
int ans = left + right;
if (left > 0 && right > 0) {
require = 0;
} else if (left == 0 && right == 0) {
ans += 1;
require = 1;
} else if (left == 0 || right == 0) {
require = 1;
}
return ans;
}
}
int navigation(TreeNode* root) {
int require = 0;
int ans = dfs(root, true, require);
if (ans == 0 && require) {
ans += 1;
}
return ans;
}
};
int main() {
freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
Solution s;
return 0;
}