Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- using namespace std;
- const int leftkey=-1000000001;
- const int rightkey=1000000001;
- struct tree{
- int m;
- int LeftChild;
- int RightChild;
- };
- bool TreeorNot(vector<tree> &array, int node, int LC, int RC) {
- bool leftbool=true;
- bool rightbool=true;
- if (( array[node].m <= LC) || (array[node].m >= RC)) {
- return false;
- } else {
- if (array[node].LeftChild!=0){
- leftbool= TreeorNot(array, array[node].LeftChild, LC, array[node].m);
- }
- if (array[node].RightChild!=0){
- leftbool= TreeorNot(array, array[node].RightChild, array[node].m, RC);
- }
- return leftbool & rightbool;
- }
- }
- int main() {
- // ifstream cin("check.in");
- // ofstream cout("check.out");
- int n;
- cin >> n;
- vector<tree> array(n);
- if (n == 0) {
- cout << "YES";
- // cin.close();
- // cout.close();
- return 0;
- } else {
- for (int i = 0; i < n; ++i) {
- cin >> array[i].m >> array[i].LeftChild >> array[i].RightChild;
- --array[i].LeftChild;
- --array[i].RightChild;
- }
- if (TreeorNot(array, 1, leftkey, rightkey)){
- cout << "YES";
- } else {
- cout << "NO";
- }
- }
- // cin.close();
- // cout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement