-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnetease2.cpp
124 lines (110 loc) · 2.32 KB
/
gnetease2.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v)
: val(v), left(nullptr), right(nullptr) {}
};
struct NodeInfo
{
TreeNode* pnode;
int left;
int right;
NodeInfo(TreeNode* p, int l, int r):
pnode(p), left(l), right(r) {}
};
// build the tree and return the root
TreeNode* BuildTree(vector<NodeInfo>& nodes)
{
vector<int> counts(nodes.size(), 0);
// link
for (size_t i = 0; i != nodes.size(); ++i)
{
int left = nodes[i].left;
int right = nodes[i].right;
if (left != -1)
{
nodes[i].pnode->left = nodes[left].pnode;
++counts[left];
}
if (right != -1)
{
nodes[i].pnode->right = nodes[right].pnode;
++counts[right];
}
}
// find root
for (size_t i = 0; i != counts.size(); ++i)
{
if (counts[i] == 0)
return nodes[i].pnode;
}
throw std::logic_error("no root");
}
// Is the tree a increasing tree?
bool IsIncrTree(TreeNode* root)
{
if (!root) return false;
vector<vector<int>> mat;
// tranverse the tree by layer
std::function<void(TreeNode*, int)> layer_travel;
layer_travel =
[&mat, &layer_travel](TreeNode* p, int depth) mutable
{
if (p == nullptr) return;
if ((int)mat.size() == depth)
mat.emplace_back(vector<int>());
mat[depth].push_back(p->val);
layer_travel(p->left, depth + 1);
layer_travel(p->right, depth + 1);
};
layer_travel(root, 0);
vector<int> sums;
for (auto& row : mat)
{
int sum = 0;
for (auto c : row) sum += c;
sums.push_back(sum);
}
for (size_t i = 1; i != sums.size(); ++i)
{
if (sums[i-1] >= sums[i])
return false;
}
return true;
}
int main()
{
int num_test;
cin >> num_test;
for (int num_nodes = 0; num_test--;)
{
cin >> num_nodes;
vector<NodeInfo> nodes;
while (num_nodes--)
{
int v, l, r;
cin >> v >> l >> r;
TreeNode* p = new TreeNode(v);
nodes.emplace_back(p, l, r);
}
TreeNode* root = BuildTree(nodes);
if (IsIncrTree(root))
cout << "YES\n";
else
cout << "NO\n";
// memory clean
for (auto& node : nodes)
{
delete node.pnode;
node.pnode = nullptr;
}
} // nodes released
return 0;
}