Language: C++




#include <iostream>
#include <vector>

#define MAX 25

using namespace std;

void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
    
    cout << ids[current] << ' ';
    
    if (lefts[current] == -1) {
        
        return;
        
    }
        
    
    printPreorder(lefts[current], lefts, rights, ids);
    
    printPreorder(rights[current], lefts, rights, ids);
    
}

int getLeftEdge(int current, vector<int>& lefts) {
    
    if (lefts[current] == -1) {
        
        return current;
        
    }
    
    return getLeftEdge(lefts[current], lefts);
    
}

void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
    
    cout << currnt << ' ';
    
    if (lefts[currnt] == -1) {
        
        return;
        
    }
    
    printChilds(lefts[currnt], lefts, rights);
    
    printChilds(rights[currnt], lefts, rights);
    
}



void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
    
    cout << current << ' ';
    
    if (current == sentinel) {
        
        if (rights[current] != -1) {
            
            printChilds(rights[current], lefts, rights);
            
        }
        
        return;
        
    }
    
    if (current == first) {
        
        printInorder(rightEdge, parents, lefts, rights, sentinel, rightEdge, first);
        
    } else {
        
        if (rights[current] != -1) {
            
            printChilds(rights[current], lefts, rights);
            
        }
        
        printInorder(parents[current], parents, lefts, rights, sentinel, rightEdge, first);
        
    }
    
}

void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
    
    cout << current << ' ';
    
    if (current == sentinel) {
        
        cout << first;
        
        return;
        
    }
    
    if (parent[current] == first) {
        
        printPostOrder(rightEdge, siblings, parent, sentinel, rightEdge, first);
        
    } else {
        
        cout << siblings[current] << ' ';
        
        printPostOrder(parent[current], siblings, parent, sentinel, rightEdge, first);
        
    }
    
}


int main(void) {
    
    int n;
    int first = 0;
    vector<int> ids(MAX, 0);
    vector<int> lefts(MAX, 0);
    vector<int> rights(MAX, 0);
    vector<int> parents(MAX, 0);
    vector<int> siblings(MAX, 0);
    
    cin >> n;
    
    parents[0] = -1;
    siblings[0] = -1;
    
    for (int i = 0; i < n; ++i) {
        
        int id; cin >> id;
        
        ids[id] = id;
        
        if (i == 0) {
            
            first = ids[id];
            
        }
        
        cin >> lefts[ids[id]];
        cin >> rights[ids[id]];
        
        parents[lefts[id]] = ids[id];
        parents[rights[id]] = ids[id];
        
        siblings[lefts[id]] = rights[id];
        siblings[rights[id]] = lefts[id];
        
    }
    
    cout << "Preorder" << endl;
    
    cout << ' ';
    
    printPreorder(first, lefts, rights, ids);
    
    cout << endl;
    
    int leftEdge = getLeftEdge(first, lefts);
    int rightEdge = getLeftEdge(rights[first], lefts);

    cout << "Inorder" << endl;
    
    cout << ' ';
    
    printInorder(leftEdge, parents, lefts, rights, rights[first], rightEdge, first);
    
    cout << endl;
    
    cout << "Postorder" << endl;
    
    cout << ' ';
    
    printPostOrder(leftEdge, siblings, parents, rights[first], rightEdge, first);
    
    cout << endl;
    
    
    return 0;
    
}






















#include <iostream>
#include <vector>

#define MAX 25

using namespace std;

void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
    
    cout << ids[current] << ' ';
    
    if (lefts[current] == -1) {
        
        return;
        
    }
        
    
    printPreorder(lefts[current], lefts, rights, ids);
    
    printPreorder(rights[current], lefts, rights, ids);
    
}

int getLeftEdge(int current, vector<int>& lefts) {
    
    if (lefts[current] == -1) {
        
        return current;
        
    }
    
    return getLeftEdge(lefts[current], lefts);
    
}

void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
    
    cout << currnt << ' ';
    
    if (lefts[currnt] == -1) {
        
        return;
        
    }
    
    printChilds(lefts[currnt], lefts, rights);
    
    printChilds(rights[currnt], lefts, rights);
    
}



void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
    
    cout << current << ' ';
    
    if (current == sentinel) {
        
        if (rights[current] != -1) {
            
            printChilds(rights[current], lefts, rights);
            
        }
        
        return;
        
    }
    
    if (current == first) {
        
        printInorder(rightEdge, parents, lefts, rights, sentinel, rightEdge, first);
        
    } else {
        
        if (rights[current] != -1) {
            
            printChilds(rights[current], lefts, rights);
            
        }
        
        printInorder(parents[current], parents, lefts, rights, sentinel, rightEdge, first);
        
    }
    
}

void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
    
    cout << current << ' ';
    
    if (current == sentinel) {
        
        cout << first;
        
        return;
        
    }
    
    if (parent[current] == first) {
        
        printPostOrder(rightEdge, siblings, parent, sentinel, rightEdge, first);
        
    } else {
        
        cout << siblings[current] << ' ';
        
        printPostOrder(parent[current], siblings, parent, sentinel, rightEdge, first);
        
    }
    
}


int main(void) {
    
    int n;
    int first = 0;
    vector<int> ids(MAX, 0);
    vector<int> lefts(MAX, 0);
    vector<int> rights(MAX, 0);
    vector<int> parents(MAX, 0);
    vector<int> siblings(MAX, 0);
    
    cin >> n;
    
    parents[0] = -1;
    siblings[0] = -1;
    
    for (int i = 0; i < n; ++i) {
        
        int id; cin >> id;
        
        ids[id] = id;
        
        if (i == 0) {
            
            first = ids[id];
            
        }
        
        cin >> lefts[ids[id]];
        cin >> rights[ids[id]];
        
        parents[lefts[id]] = ids[id];
        parents[rights[id]] = ids[id];
        
        siblings[lefts[id]] = rights[id];
        siblings[rights[id]] = lefts[id];
        
    }
    
    cout << "Preorder" << endl;
    
    cout << ' ';
    
    printPreorder(first, lefts, rights, ids);
    
    cout << endl;
    
    int leftEdge = getLeftEdge(first, lefts);
    int rightEdge = getLeftEdge(rights[first], lefts);

    cout << "Inorder" << endl;
    
    cout << ' ';
    
    printInorder(leftEdge, parents, lefts, rights, rights[first], rightEdge, first);
    
    cout << endl;
    
    cout << "Postorder" << endl;
    
    cout << ' ';
    
    printPostOrder(leftEdge, siblings, parents, rights[first], rightEdge, first);
    
    cout << endl;
    
    
    return 0;
    
}






























#include <iostream>
#include <vector>

#define MAX 25

using namespace std;

void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
    
    cout << ids[current] << ' ';
    
    if (lefts[current] == -1) {
        
        return;
        
    }
        
    
    printPreorder(lefts[current], lefts, rights, ids);
    
    printPreorder(rights[current], lefts, rights, ids);
    
}

int getLeftEdge(int current, vector<int>& lefts) {
    
    if (lefts[current] == -1) {
        
        return current;
        
    }
    
    return getLeftEdge(lefts[current], lefts);
    
}

void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
    
    cout << currnt << ' ';
    
    if (lefts[currnt] == -1) {
        
        return;
        
    }
    
    printChilds(lefts[currnt], lefts, rights);
    
    printChilds(rights[currnt], lefts, rights);
    
}



void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
    
    cout << current << ' ';
    
    if (current == sentinel) {
        
        if (rights[current] != -1) {
            
            printChilds(rights[current], lefts, rights);
            
        }
        
        return;
        
    }
    
    if (current == first) {
        
        printInorder(rightEdge, parents, lefts, rights, sentinel, rightEdge, first);
        
    } else {
        
        if (rights[current] != -1) {
            
            printChilds(rights[current], lefts, rights);
            
        }
        
        printInorder(parents[current], parents, lefts, rights, sentinel, rightEdge, first);
        
    }
    
}

void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
    
    cout << current << ' ';
    
    if (current == sentinel) {
        
        cout << first;
        
        return;
        
    }
    
    if (parent[current] == first) {
        
        printPostOrder(rightEdge, siblings, parent, sentinel, rightEdge, first);
        
    } else {
        
        cout << siblings[current] << ' ';
        
        printPostOrder(parent[current], siblings, parent, sentinel, rightEdge, first);
        
    }
    
}


int main(void) {
    
    int n;
    int first = 0;
    vector<int> ids(MAX, 0);
    vector<int> lefts(MAX, 0);
    vector<int> rights(MAX, 0);
    vector<int> parents(MAX, 0);
    vector<int> siblings(MAX, 0);
    
    cin >> n;
    
    parents[0] = -1;
    siblings[0] = -1;
    
    for (int i = 0; i < n; ++i) {
        
        int id; cin >> id;
        
        ids[id] = id;
        
        if (i == 0) {
            
            first = ids[id];
            
        }
        
        cin >> lefts[ids[id]];
        cin >> rights[ids[id]];
        
        parents[lefts[id]] = ids[id];
        parents[rights[id]] = ids[id];
        
        siblings[lefts[id]] = rights[id];
        siblings[rights[id]] = lefts[id];
        
    }
    
    cout << "Preorder" << endl;
    
    cout << ' ';
    
    printPreorder(first, lefts, rights, ids);
    
    cout << endl;
    
    int leftEdge = getLeftEdge(first, lefts);
    int rightEdge = getLeftEdge(rights[first], lefts);

    cout << "Inorder" << endl;
    
    cout << ' ';
    
    printInorder(leftEdge, parents, lefts, rights, rights[first], rightEdge, first);
    
    cout << endl;
    
    cout << "Postorder" << endl;
    
    cout << ' ';
    
    printPostOrder(leftEdge, siblings, parents, rights[first], rightEdge, first);
    
    cout << endl;
    
    
    return 0;
    
}































#include <iostream>
#include <vector>

#define MAX 25

using namespace std;

void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
    
    cout << ids[current] << ' ';
    
    if (lefts[current] == -1) {
        
        return;
        
    }
        
    
    printPreorder(lefts[current], lefts, rights, ids);
    
    printPreorder(rights[current], lefts, rights, ids);
    
}

int getLeftEdge(int current, vector<int>& lefts) {
    
    if (lefts[current] == -1) {
        
        return current;
        
    }
    
    return getLeftEdge(lefts[current], lefts);
    
}

void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
    
    cout << currnt << ' ';
    
    if (lefts[currnt] == -1) {
        
        return;
        
    }
    
    printChilds(lefts[currnt], lefts, rights);
    
    printChilds(rights[currnt], lefts, rights);
    
}



void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
    
    cout << current << ' ';
    
    if (current == sentinel) {
        
        if (rights[current] != -1) {
            
            printChilds(rights[current], lefts, rights);
            
        }
        
        return;
        
    }
    
    if (current == first) {
        
        printInorder(rightEdge, parents, lefts, rights, sentinel, rightEdge, first);
        
    } else {
        
        if (rights[current] != -1) {
            
            printChilds(rights[current], lefts, rights);
            
        }
        
        printInorder(parents[current], parents, lefts, rights, sentinel, rightEdge, first);
        
    }
    
}

void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
    
    cout << current << ' ';
    
    if (current == sentinel) {
        
        cout << first;
        
        return;
        
    }
    
    if (parent[current] == first) {
        
        printPostOrder(rightEdge, siblings, parent, sentinel, rightEdge, first);
        
    } else {
        
        cout << siblings[current] << ' ';
        
        printPostOrder(parent[current], siblings, parent, sentinel, rightEdge, first);
        
    }
    
}


int main(void) {
    
    int n;
    int first = 0;
    vector<int> ids(MAX, 0);
    vector<int> lefts(MAX, 0);
    vector<int> rights(MAX, 0);
    vector<int> parents(MAX, 0);
    vector<int> siblings(MAX, 0);
    
    cin >> n;
    
    parents[0] = -1;
    siblings[0] = -1;
    
    for (int i = 0; i < n; ++i) {
        
        int id; cin >> id;
        
        ids[id] = id;
        
        if (i == 0) {
            
            first = ids[id];
            
        }
        
        cin >> lefts[ids[id]];
        cin >> rights[ids[id]];
        
        parents[lefts[id]] = ids[id];
        parents[rights[id]] = ids[id];
        
        siblings[lefts[id]] = rights[id];
        siblings[rights[id]] = lefts[id];
        
    }
    
    cout << "Preorder" << endl;
    
    cout << ' ';
    
    printPreorder(first, lefts, rights, ids);
    
    cout << endl;
    
    int leftEdge = getLeftEdge(first, lefts);
    int rightEdge = getLeftEdge(rights[first], lefts);

    cout << "Inorder" << endl;
    
    cout << ' ';
    
    printInorder(leftEdge, parents, lefts, rights, rights[first], rightEdge, first);
    
    cout << endl;
    
    cout << "Postorder" << endl;
    
    cout << ' ';
    
    printPostOrder(leftEdge, siblings, parents, rights[first], rightEdge, first);
    
    cout << endl;
    
    
    return 0;
    
}

Compiler output:
a.cc:217:6: error: redefinition of 'void printPreorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&)'
  217 | void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
      |      ^~~~~~~~~~~~~
a.cc:11:6: note: 'void printPreorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&)' previously defined here
   11 | void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
      |      ^~~~~~~~~~~~~
a.cc:234:5: error: redefinition of 'int getLeftEdge(int, std::vector<int>&)'
  234 | int getLeftEdge(int current, vector<int>& lefts) {
      |     ^~~~~~~~~~~
a.cc:28:5: note: 'int getLeftEdge(int, std::vector<int>&)' previously defined here
   28 | int getLeftEdge(int current, vector<int>& lefts) {
      |     ^~~~~~~~~~~
a.cc:246:6: error: redefinition of 'void printChilds(int, std::vector<int>&, std::vector<int>&)'
  246 | void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
      |      ^~~~~~~~~~~
a.cc:40:6: note: 'void printChilds(int, std::vector<int>&, std::vector<int>&)' previously defined here
   40 | void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
      |      ^~~~~~~~~~~
a.cc:264:6: error: redefinition of 'void printInorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&, int, int, int)'
  264 | void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~
a.cc:58:6: note: 'void printInorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&, int, int, int)' previously defined here
   58 | void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~
a.cc:298:6: error: redefinition of 'void printPostOrder(int, std::vector<int>&, std::vector<int>&, int, int, int)'
  298 | void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~~~
a.cc:92:6: note: 'void printPostOrder(int, std::vector<int>&, std::vector<int>&, int, int, int)' previously defined here
   92 | void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~~~
a.cc:325:5: error: redefinition of 'int main()'
  325 | int main(void) {
      |     ^~~~
a.cc:119:5: note: 'int main()' previously defined here
  119 | int main(void) {
      |     ^~~~
a.cc:431:6: error: redefinition of 'void printPreorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&)'
  431 | void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
      |      ^~~~~~~~~~~~~
a.cc:11:6: note: 'void printPreorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&)' previously defined here
   11 | void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
      |      ^~~~~~~~~~~~~
a.cc:448:5: error: redefinition of 'int getLeftEdge(int, std::vector<int>&)'
  448 | int getLeftEdge(int current, vector<int>& lefts) {
      |     ^~~~~~~~~~~
a.cc:28:5: note: 'int getLeftEdge(int, std::vector<int>&)' previously defined here
   28 | int getLeftEdge(int current, vector<int>& lefts) {
      |     ^~~~~~~~~~~
a.cc:460:6: error: redefinition of 'void printChilds(int, std::vector<int>&, std::vector<int>&)'
  460 | void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
      |      ^~~~~~~~~~~
a.cc:40:6: note: 'void printChilds(int, std::vector<int>&, std::vector<int>&)' previously defined here
   40 | void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
      |      ^~~~~~~~~~~
a.cc:478:6: error: redefinition of 'void printInorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&, int, int, int)'
  478 | void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~
a.cc:58:6: note: 'void printInorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&, int, int, int)' previously defined here
   58 | void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~
a.cc:512:6: error: redefinition of 'void printPostOrder(int, std::vector<int>&, std::vector<int>&, int, int, int)'
  512 | void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~~~
a.cc:92:6: note: 'void printPostOrder(int, std::vector<int>&, std::vector<int>&, int, int, int)' previously defined here
   92 | void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~~~
a.cc:539:5: error: redefinition of 'int main()'
  539 | int main(void) {
      |     ^~~~
a.cc:119:5: note: 'int main()' previously defined here
  119 | int main(void) {
      |     ^~~~
a.cc:646:6: error: redefinition of 'void printPreorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&)'
  646 | void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
      |      ^~~~~~~~~~~~~
a.cc:11:6: note: 'void printPreorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&)' previously defined here
   11 | void printPreorder(int current, vector<int>& lefts, vector<int>& rights, vector<int>& ids) {
      |      ^~~~~~~~~~~~~
a.cc:663:5: error: redefinition of 'int getLeftEdge(int, std::vector<int>&)'
  663 | int getLeftEdge(int current, vector<int>& lefts) {
      |     ^~~~~~~~~~~
a.cc:28:5: note: 'int getLeftEdge(int, std::vector<int>&)' previously defined here
   28 | int getLeftEdge(int current, vector<int>& lefts) {
      |     ^~~~~~~~~~~
a.cc:675:6: error: redefinition of 'void printChilds(int, std::vector<int>&, std::vector<int>&)'
  675 | void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
      |      ^~~~~~~~~~~
a.cc:40:6: note: 'void printChilds(int, std::vector<int>&, std::vector<int>&)' previously defined here
   40 | void printChilds(int currnt, vector<int>& lefts, vector<int>& rights) {
      |      ^~~~~~~~~~~
a.cc:693:6: error: redefinition of 'void printInorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&, int, int, int)'
  693 | void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~
a.cc:58:6: note: 'void printInorder(int, std::vector<int>&, std::vector<int>&, std::vector<int>&, int, int, int)' previously defined here
   58 | void printInorder(int current, vector<int>& parents, vector<int>& lefts, vector<int>& rights, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~
a.cc:727:6: error: redefinition of 'void printPostOrder(int, std::vector<int>&, std::vector<int>&, int, int, int)'
  727 | void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~~~
a.cc:92:6: note: 'void printPostOrder(int, std::vector<int>&, std::vector<int>&, int, int, int)' previously defined here
   92 | void printPostOrder(int current, vector<int>& siblings, vector<int>& parent, int sentinel, int rightEdge, int first) {
      |      ^~~~~~~~~~~~~~
a.cc:754:5: error: redefinition of 'int main()'
  754 | int main(void) {
      |     ^~~~
a.cc:119:5: note: 'int main()' previously defined here
  119 | int main(void) {
      |     ^~~~