Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #define INPUT_FILE "check.in"
- #define OUTPUT_FILE "check.out"
- using namespace std;
- struct tree {
- int value, left, right;
- };
- string check(tree *Tree, int index) {
- string result = "YES";
- if (Tree[index].left - 1 > 0) {
- if (Tree[index].value > Tree[Tree[index].left - 1].value) {
- result = check(Tree, Tree[index].left - 1);
- } else result = "NO";
- }
- if (Tree[index].right - 1 > 0) {
- if (Tree[index].value < Tree[Tree[index].right - 1].value) {
- result = check(Tree, Tree[index].right - 1);
- } else result = "NO";
- }
- return result;
- }
- int main(int argc, const char * argv[]) {
- ifstream inputFile(INPUT_FILE);
- ofstream outputFile(OUTPUT_FILE);
- int n;
- inputFile >> n;
- if (n > 0) {
- tree *Tree = new tree[n];
- for (int i = 0; i < n; i++) {
- inputFile >> Tree[i].value >> Tree[i].left >> Tree[i].right;
- }
- outputFile << check(Tree, 0);
- } else {
- outputFile << "YES";
- }
- inputFile.close();
- outputFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement