Language: C++

#include <iostream> 
#include <map> 
#include <set>
using namespace std;

typedef struct node {
????int id;
????node * left, * right;
????node(int id): id(id), left(NULL), right(NULL) {}
}Node;
class Solution {
????private: map < int, Node * > hashMapping;
????set < int > rootCandidates, nonRootNodes;
????Node * createNode(int ID) {
????????if (ID != -1) {
????????????if (hashMapping.find(ID) == hashMapping.end()) {
????????????????hashMapping[ID] = new Node(ID);
????????????}
????????????return hashMapping[ID];
????????} else {
????????????return NULL;
????????}
????}
????void preorder(Node * root) {
????????while (root) {
????????????while (root - > left && root - > right) {
????????????????Node * currNode = root - > left;
????????????????while (currNode - > right != NULL && 
???????????????????????currNode - > right != root) {
????????????????????currNode = currNode - > right;
????????????????}
????????????????if (currNode - > right == NULL) {
????????????????????currNode - > right = root;
????????????????????break;
????????????????} else {
????????????????????currNode - > right = NULL;
????????????????????root = root - > right;
????????????????}
????????????}
????????????cout << " " << root - > id;
????????????root = root - > left ? root - > left : root - > right;
????????}
????}
????void inorder(Node * root) {
????????while (root) {
????????????while (root - > left) {
????????????????Node * currNode = root - > left;
????????????????while (currNode - > right != NULL && 
???????????????????????currNode - > right != root) {
????????????????????currNode = currNode - > right;
????????????????}
????????????????if (currNode - > right == NULL) {
????????????????????currNode - > right = root;
????????????????????root = root - > left;
????????????????} else {
????????????????????currNode - > right = NULL;
????????????????????break;
????????????????}
????????????}
????????????cout << " " << root - > id;
????????????root = root - > right;
????????}
????}
????void reverse(Node * from, Node * to) {
????????Node * a = NULL, * b = from, * c = from - > right;
????????while (a != to) {
????????????b - > right = a;
????????????a = b;
????????????b = c;
????????????if (c) {
????????????????c = c - > right;
????????????}
????????}
????}
????void postorder(Node * root) {
????????Node * forgeRoot = new Node(-1);
????????forgeRoot - > left = root;
????????root = forgeRoot;
????????do {
????????????while (root - > left) {
????????????????Node * currNode = root - > left;
????????????????while (currNode - > right != NULL && 
???????????????????????currNode - > right != root) {
????????????????????currNode = currNode - > right;
????????????????}
????????????????if (currNode - > right == NULL) {
????????????????????currNode - > right = root;
????????????????????root = root - > left;
????????????????} else {
????????????????????currNode - > right = NULL;
????????????????????root = root - > right;
????????????????}
????????????}
????????????Node * from = root - > right - > left, * to = root;
????????????reverse(from, to);
????????????while (from != to) {
????????????????cout << " " << from - > id;
????????????????from = from - > right;
????????????}
????????????cout << " " << from - > id;
????????????reverse(from, to);
????????????root = root - > right;
????????} while (root != forgeRoot);
????}
????public: 
??????node * buildTree() {
????????hashMapping.clear();
????????rootCandidates.clear();
????????nonRootNodes.clear();
????????int num, rootID = -1;
????????cin >> num;
????????while (num--) {
????????????int ID, leftID, rightID;
????????????cin >> ID >> leftID >> rightID;
????????????if (nonRootNodes.find(ID) == nonRootNodes.end()) {
????????????????rootCandidates.insert(ID);
????????????}
????????????nonRootNodes.insert(leftID);
????????????rootCandidates.erase(leftID);
????????????nonRootNodes.insert(rightID);
????????????rootCandidates.erase(rightID);
????????????createNode(ID) - > left = createNode(leftID);
????????????createNode(ID) - > right = createNode(rightID);
????????}
????????return hashMapping[ * rootCandidates.begin()];
????}
????void printPreOrder(node * root) {
????????cout << "Preorder" << endl;
????????preorder(root);
????????cout << endl;
????}
????void printInOrder(node * root) {
????????cout << "Inorder" << endl;
????????inorder(root);
????????cout << endl;
????}
????void printPostOrder(node * root) {
????????cout << "Postorder" << endl;
????????postorder(root);
????????cout << endl;
????}
}sln;
int main(int argc, char * argv[]) {
????ios::sync_with_stdio(false);
????node * root = sln.buildTree();
????sln.printPreOrder(root);
????sln.printInOrder(root);
????sln.printPostOrder(root);
????return 0;
}

Compiler output:
a.cc:7:1: error: expected unqualified-id before '?' token
    7 | ????int id;
      | ^
a.cc:8:1: error: expected unqualified-id before '?' token
    8 | ????node * left, * right;
      | ^
a.cc:9:1: error: expected unqualified-id before '?' token
    9 | ????node(int id): id(id), left(NULL), right(NULL) {}
      | ^
a.cc:12:1: error: expected unqualified-id before '?' token
   12 | ????private: map < int, Node * > hashMapping;
      | ^
a.cc:13:1: error: expected unqualified-id before '?' token
   13 | ????set < int > rootCandidates, nonRootNodes;
      | ^
a.cc:14:1: error: expected unqualified-id before '?' token
   14 | ????Node * createNode(int ID) {
      | ^
a.cc:24:1: error: expected unqualified-id before '?' token
   24 | ????void preorder(Node * root) {
      | ^
a.cc:44:1: error: expected unqualified-id before '?' token
   44 | ????void inorder(Node * root) {
      | ^
a.cc:64:1: error: expected unqualified-id before '?' token
   64 | ????void reverse(Node * from, Node * to) {
      | ^
a.cc:75:1: error: expected unqualified-id before '?' token
   75 | ????void postorder(Node * root) {
      | ^
a.cc:105:1: error: expected unqualified-id before '?' token
  105 | ????public:
      | ^
a.cc:127:1: error: expected unqualified-id before '?' token
  127 | ????void printPreOrder(node * root) {
      | ^
a.cc:132:1: error: expected unqualified-id before '?' token
  132 | ????void printInOrder(node * root) {
      | ^
a.cc:137:1: error: expected unqualified-id before '?' token
  137 | ????void printPostOrder(node * root) {
      | ^
a.cc: In function 'int main(int, char**)':
a.cc:144:1: error: expected primary-expression before '?' token
  144 | ????ios::sync_with_stdio(false);
      | ^
a.cc:144:2: error: expected primary-expression before '?' token
  144 | ????ios::sync_with_stdio(false);
      |  ^
a.cc:144:3: error: expected primary-expression before '?' token
  144 | ????ios::sync_with_stdio(false);
      |   ^
a.cc:144:4: error: expected primary-expression before '?' token
  144 | ????ios::sync_with_stdio(false);
      |    ^
a.cc:144:32: error: expected ':' before ';' token
  144 | ????ios::sync_with_stdio(false);
      |                                ^
      |                                :
a.cc:144:32: error: expected primary-expression before ';' token
a.cc:144:32: error: expected ':' before ';' token
  144 | ????ios::sync_with_stdio(false);
      |                                ^
      |                                :
a.cc:144:32: error: expected primary-expression before ';' token
a.cc:144:32: error: expected ':' before ';' token
  144 | ????ios::sync_with_stdio(false);
      |                                ^
      |                                :
a.cc:144:32: error: expected primary-expression before ';' token
a.cc:144:32: error: expected ':' before ';' token
  144 | ????ios::sync_with_stdio(false);
      |                                ^
      |                                :
a.cc:144:32: error: expected primary-expression before ';' token
a.cc:145:1: error: expected primary-expression before '?' token
  145 | ????node * root = sln.buildTree();
      | ^
a.cc:145:2: error: expected primary-expression before '?' token
  145 | ????node * root = sln.buildTree();
      |  ^
a.cc:145:3: error: expected primary-expression before '?' token
  145 | ????node * root = sln.buildTree();
      |   ^
a.cc:145:4: error: expected primary-expression before '?' token
  145 | ????node * root = sln.buildTree();
      |    ^
a.cc:145:10: error: expected primary-expression before '*' token
  145 | ????node * root = sln.buildTree();
      |          ^
a.cc:145:12: error: 'root' was not declared in this scope
  145 | ????node * root = sln.buildTree();
      |            ^~~~
a.cc:145:23: error: 'class Solution' has no member named 'buildTree'
  145 | ????node * root = sln.buildTree();
      |                       ^~~~~~~~~
a.cc:145:34: error: expected ':' before ';' token
  145 | ????node * root = sln.buildTree();
      |                                  ^
      |                                  :
a.cc:145:34: error: expected primary-expression before ';' token
a.cc:145:34: error: expected ':' before ';' token
  145 | ????node * root = sln.buildTree();
      |                                  ^
      |                                  :
a.cc:145:34: error: expected primary-expression before ';' token
a.cc:145:34: error: expected ':' before ';' token
  145 | ????node * root = sln.buildTree();
      |                                  ^
      |                                  :
a.cc:145:34: error: expected primary-expression before ';' token
a.cc:145:34: error: expected ':' before ';' token
  145 | ????node * root = sln.buildTree();
      |                                  ^
      |                                  :
a.cc:145:34: error: expected primary-expression before ';' token
a.cc:146:1: error: expected primary-expression before '?' token
  146 | ????sln.printPreOrder(root);
      | ^
a.cc:146:2: error: expected primary-expression before '?' token
  146 | ????sln.printPreOrder(root);
      |  ^
a.cc:146:3: error: expected primary-expression before '?' token
  146 | ????sln.printPreOrder(root);
      |   ^
a.cc:146:4: error: expected primary-expression before '?' token
  146 | ????sln.printPreOrder(root);
      |    ^
a.cc:146:9: error: 'class Solution' has no member named 'printPreOrder'
  146 | ????sln.printPreOrder(root);
      |         ^~~~~~~~~~~~~
a.cc:146:28: error: expected ':' before ';' token
  146 | ????sln.printPreOrder(root);
      |                            ^
      |                            :
a.cc:146:28: error: expected primary-expression before ';' token
a.cc:146:28: error: expected ':' before ';' token
  146 | ????sln.printPreOrder(root);
      |                            ^
      |                            :
a.cc:146:28: error: expected primary-expression before ';' token
a.cc:146:28: error: expected ':' before ';' token
  146 | ????sln.printPreOrder(root);
      |                            ^
      |                            :
a.cc:146:28: error: expected primary-expression before ';' token
a.cc:146:28: error: expected ':' before ';' token
  146 | ????sln.printPreOrder(root);
      |                            ^
      |                            :
a.cc:146:28: error: expected primary-expression before ';' token
a.cc:147:1: error: expected primary-expression before '?' token
  147 | ????sln.printInOrder(root);
      | ^
a.cc:147:2: error: expected primary-expression before '?' token
  147 | ????sln.printInOrder(root);
      |  ^
a.cc:147:3: error: expected primary-expression before '?' token
  147 | ????sln.printInOrder(root);
      |   ^
a.cc:147:4: error: expected primary-expression before '?' token
  147 | ????sln.printInOrder(root);
      |    ^
a.cc:147:9: error: 'class Solution' has no member named 'printInOrder'
  147 | ????sln.printInOrder(root);
      |         ^~~~~~~~~~~~
a.cc:147:27: error: expected ':' before ';' token
  147 | ????sln.printInOrder(root);
      |                           ^
      |                           :
a.cc:147:27: error: expected primary-expression before ';' token
a.cc:147:27: error: expected ':' before ';' token
  147 | ????sln.printInOrder(root);
      |                           ^
      |                           :
a.cc:147:27: error: expected primary-expression before ';' token
a.cc:147:27: error: expected ':' before ';' token
  147 | ????sln.printInOrder(root);
      |                           ^
      |                           :
a.cc:147:27: error: expected primary-expression before ';' token
a.cc:147:27: error: expected ':' before ';' token
  147 | ????sln.printInOrder(root);
      |                           ^
      |                           :
a.cc:147:27: error: expected primary-expression before ';' token
a.cc:148:1: error: expected primary-expression before '?' token
  148 | ????sln.printPostOrder(root);
      | ^
a.cc:148:2: error: expected primary-expression before '?' token
  148 | ????sln.printPostOrder(root);
      |  ^
a.cc:148:3: error: expected primary-expression before '?' token
  148 | ????sln.printPostOrder(root);
      |   ^
a.cc:148:4: error: expected primary-expression before '?' token
  148 | ????sln.printPostOrder(root);
      |    ^
a.cc:148:9: error: 'class Solution' has no member named 'printPostOrder'
  148 | ????sln.printPostOrder(root);
      |         ^~~~~~~~~~~~~~
a.cc:148:29: error: expected ':' before ';' token
  148 | ????sln.printPostOrder(root);
      |                             ^
      |                             :
a.cc:148:29: error: expected primary-expression before ';' token
a.cc:148:29: error: expected ':' before ';' token
  148 | ????sln.printPostOrder(root);
      |                             ^
      |                             :
a.cc:148:29: error: expected primary-expression before ';' token
a.cc:148:29: error: expected ':' before ';' token
  148 | ????sln.printPostOrder(root);
      |                             ^
      |                             :
a.cc:148:29: error: expected primary-expression before ';' token
a.cc:148:29: error: expected ':' before ';' token
  148 | ????sln.printPostOrder(root);
      |                             ^
      |                             :
a.cc:148:29: error: expected primary-expression before ';' token
a.cc:149:1: error: expected primary-expression before '?' token
  149 | ????return 0;
      | ^
a.cc:149:2: error: expected primary-expression before '?' token
  149 | ????return 0;
      |  ^
a.cc:149:3: error: expected primary-expression before '?' token
  149 | ????return 0;
      |   ^
a.cc:149:4: error: expected primary-expression before '?' token
  149 | ????return 0;
      |    ^
a.cc:149:5: error: expected primary-expression before 'return'
  149 | ????return 0;
      |     ^~~~~~
a.cc:149:5: error: expected ':' before 'return'
  149 | ????return 0;
      |     ^~~~~~
      |     :
a.cc:149:5: error: expected primary-expression before 'return'
  149 | ????return 0;
      |     ^~~~~~
a.cc:149:5: error: expected ':' before 'return'
  149 | ????return 0;
      |     ^~~~~~
      |     :
a.cc:149:5: error: expected primary-expression before 'return'
  149 | ????return