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: In member function 'void Solution::preorder(Node*)':
a.cc:25:15: error: expected primary-expression before '>' token
   25 | while (root - > left && root - > right) {
      |               ^
a.cc:25:32: error: expected primary-expression before '>' token
   25 | while (root - > left && root - > right) {
      |                                ^
a.cc:26:26: error: expected primary-expression before '>' token
   26 | Node * currNode = root - > left;
      |                          ^
a.cc:27:19: error: expected primary-expression before '>' token
   27 | while (currNode - > right != NULL &&
      |                   ^
a.cc:28:12: error: expected primary-expression before '>' token
   28 | currNode - > right != root) {
      |            ^
a.cc:29:23: error: expected primary-expression before '>' token
   29 | currNode = currNode - > right;
      |                       ^
a.cc:31:16: error: expected primary-expression before '>' token
   31 | if (currNode - > right == NULL) {
      |                ^
a.cc:32:12: error: expected primary-expression before '>' token
   32 | currNode - > right = root;
      |            ^
a.cc:35:12: error: expected primary-expression before '>' token
   35 | currNode - > right = NULL;
      |            ^
a.cc:36:15: error: expected primary-expression before '>' token
   36 | root = root - > right;
      |               ^
a.cc:39:23: error: expected primary-expression before '>' token
   39 | cout << " " << root - > id;
      |                       ^
a.cc:39:25: error: 'id' was not declared in this scope; did you mean 'void'?
   39 | cout << " " << root - > id;
      |                         ^~
      |                         void
a.cc:40:15: error: expected primary-expression before '>' token
   40 | root = root - > left ? root - > left : root - > right;
      |               ^
a.cc:40:31: error: expected primary-expression before '>' token
   40 | root = root - > left ? root - > left : root - > right;
      |                               ^
a.cc:40:47: error: expected primary-expression before '>' token
   40 | root = root - > left ? root - > left : root - > right;
      |                                               ^
a.cc: In member function 'void Solution::inorder(Node*)':
a.cc:45:15: error: expected primary-expression before '>' token
   45 | while (root - > left) {
      |               ^
a.cc:46:26: error: expected primary-expression before '>' token
   46 | Node * currNode = root - > left;
      |                          ^
a.cc:47:19: error: expected primary-expression before '>' token
   47 | while (currNode - > right != NULL &&
      |                   ^
a.cc:48:12: error: expected primary-expression before '>' token
   48 | currNode - > right != root) {
      |            ^
a.cc:49:23: error: expected primary-expression before '>' token
   49 | currNode = currNode - > right;
      |                       ^
a.cc:51:16: error: expected primary-expression before '>' token
   51 | if (currNode - > right == NULL) {
      |                ^
a.cc:52:12: error: expected primary-expression before '>' token
   52 | currNode - > right = root;
      |            ^
a.cc:53:15: error: expected primary-expression before '>' token
   53 | root = root - > left;
      |               ^
a.cc:55:12: error: expected primary-expression before '>' token
   55 | currNode - > right = NULL;
      |            ^
a.cc:59:23: error: expected primary-expression before '>' token
   59 | cout << " " << root - > id;
      |                       ^
a.cc:59:25: error: 'id' was not declared in this scope; did you mean 'void'?
   59 | cout << " " << root - > id;
      |                         ^~
      |                         void
a.cc:60:15: error: expected primary-expression before '>' token
   60 | root = root - > right;
      |               ^
a.cc: In member function 'void Solution::reverse(Node*, Node*)':
a.cc:64:43: error: expected primary-expression before '>' token
   64 | Node * a = NULL, * b = from, * c = from - > right;
      |                                           ^
a.cc:66:5: error: expected primary-expression before '>' token
   66 | b - > right = a;
      |     ^
a.cc:70:9: error: expected primary-expression before '>' token
   70 | c = c - > right;
      |         ^
a.cc: In member function 'void Solution::postorder(Node*)':
a.cc:76:13: error: expected primary-expression before '>' token
   76 | forgeRoot - > left = root;
      |             ^
a.cc:79:15: error: expected primary-expression before '>' token
   79 | while (root - > left) {
      |               ^
a.cc:80:26: error: expected primary-expression before '>' token
   80 | Node * currNode = root - > left;
      |                          ^
a.cc:81:19: error: expected primary-expression before '>' token
   81 | while (currNode - > right != NULL &&
      |                   ^
a.cc:82:12: error: expected primary-expression before '>' token
   82 | currNode - > right != root) {
      |            ^
a.cc:83:23: error: expected primary-expression before '>' token
   83 | currNode = currNode - > right;
      |                       ^
a.cc:85:16: error: expected primary-expression before '>' token
   85 | if (currNode - > right == NULL) {
      |                ^
a.cc:86:12: error: expected primary-expression before '>' token
   86 | currNode - > right = root;
      |            ^
a.cc:87:15: error: expected primary-expression before '>' token
   87 | root = root - > left;
      |               ^
a.cc:89:12: error: expected primary-expression before '>' token
   89 | currNode - > right = NULL;
      |            ^
a.cc:90:15: error: expected primary-expression before '>' token
   90 | root = root - > right;
      |               ^
a.cc:93:22: error: expected primary-expression before '>' token
   93 | Node * from = root - > right - > left, * to = root;
      |                      ^
a.cc:93:32: error: expected primary-expression before '>' token
   93 | Node * from = root - > right - > left, * to = root;
      |                                ^
a.cc:94:15: error: 'to' was not declared in this scope; did you mean 'tm'?
   94 | reverse(from, to);
      |               ^~
      |               tm
a.cc:96:23: error: expected primary-expression before '>' token
   96 | cout << " " << from - > id;
      |                       ^
a.cc:96:25: error: 'id' was not declared in this scope; did you mean 'void'?
   96 | cout << " " << from - > id;
      |                         ^~
      |                         void
a.cc:97:15: error: expected primary-expression before '>' token
   97 | from = from - > right;
      |               ^
a.cc:99:23: error: expected primary-expression before '>' token
   99 | cout << " " << from - > id;
      |                       ^
a.cc:99:25: error: 'id' was not declared in this scope; did you mean 'void'?
   99 | cout << " " << from - > id;
      |                         ^~
      |                         void
a.cc:101:15: error: expected primary-expression before '>' token
  101 | root = root - > right;
      |               ^
a.cc: In member function 'node* Solution::buildTree()':
a.cc:121:18: error: expected primary-expression before '>' token
  121 | createNode(ID) - > left = createNode(leftID);
      |                  ^
a.cc:122:18: error: expected primary-expression before '>' token
  122 | createNode(ID) - > right = createNode(rightID);
      |                  ^