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:5:1: error: expected unqualified-id before '?' token
    5 | ?
      | ^
a.cc:10:2: error: 'Node' does not name a type
   10 | }Node;
      |  ^~~~
a.cc:12:21: error: 'Node' was not declared in this scope
   12 | private: map < int, Node * > hashMapping;
      |                     ^~~~
a.cc:12:28: error: template argument 2 is invalid
   12 | private: map < int, Node * > hashMapping;
      |                            ^
a.cc:12:28: error: template argument 4 is invalid
a.cc:14:1: error: 'Node' does not name a type
   14 | Node * createNode(int ID) {
      | ^~~~
a.cc:24:15: error: 'Node' has not been declared
   24 | void preorder(Node * root) {
      |               ^~~~
a.cc:44:14: error: 'Node' has not been declared
   44 | void inorder(Node * root) {
      |              ^~~~
a.cc:64:14: error: 'Node' has not been declared
   64 | void reverse(Node * from, Node * to) {
      |              ^~~~
a.cc:64:27: error: 'Node' has not been declared
   64 | void reverse(Node * from, Node * to) {
      |                           ^~~~
a.cc:75:16: error: 'Node' has not been declared
   75 | void postorder(Node * root) {
      |                ^~~~
a.cc:106:1: error: 'node' does not name a type
  106 | node * buildTree() {
      | ^~~~
a.cc:127:20: error: 'node' has not been declared
  127 | void printPreOrder(node * root) {
      |                    ^~~~
a.cc:132:19: error: 'node' has not been declared
  132 | void printInOrder(node * root) {
      |                   ^~~~
a.cc:137:21: error: 'node' has not been declared
  137 | void printPostOrder(node * root) {
      |                     ^~~~
a.cc: In member function 'void Solution::preorder(int*)':
a.cc:26:15: error: expected primary-expression before '>' token
   26 | while (root - > left && root - > right) {
      |               ^
a.cc:26:32: error: expected primary-expression before '>' token
   26 | while (root - > left && root - > right) {
      |                                ^
a.cc:27:1: error: 'Node' was not declared in this scope
   27 | Node * currNode = root - > left;
      | ^~~~
a.cc:27:8: error: 'currNode' was not declared in this scope
   27 | Node * currNode = root - > left;
      |        ^~~~~~~~
a.cc:27:26: error: expected primary-expression before '>' token
   27 | Node * currNode = root - > left;
      |                          ^
a.cc:28:19: error: expected primary-expression before '>' token
   28 | while (currNode - > right != NULL &&
      |                   ^
a.cc:29:12: error: expected primary-expression before '>' token
   29 | currNode - > right != root) {
      |            ^
a.cc:30:23: error: expected primary-expression before '>' token
   30 | currNode = currNode - > right;
      |                       ^
a.cc:32:16: error: expected primary-expression before '>' token
   32 | if (currNode - > right == NULL) {
      |                ^
a.cc:33:12: error: expected primary-expression before '>' token
   33 | currNode - > right = root;
      |            ^
a.cc:36:12: error: expected primary-expression before '>' token
   36 | currNode - > right = NULL;
      |            ^
a.cc:37:15: error: expected primary-expression before '>' token
   37 | root = root - > right;
      |               ^
a.cc:40:23: error: expected primary-expression before '>' token
   40 | cout << " " << root - > id;
      |                       ^
a.cc:40:25: error: 'id' was not declared in this scope; did you mean 'void'?
   40 | cout << " " << root - > id;
      |                         ^~
      |                         void
a.cc:41:15: error: expected primary-expression before '>' token
   41 | root = root - > left ? root - > left : root - > right;
      |               ^
a.cc:41:31: error: expected primary-expression before '>' token
   41 | root = root - > left ? root - > left : root - > right;
      |                               ^
a.cc:41:47: error: expected primary-expression before '>' token
   41 | root = root - > left ? root - > left : root - > right;
      |                                               ^
a.cc: In member function 'void Solution::inorder(int*)':
a.cc:46:15: error: expected primary-expression before '>' token
   46 | while (root - > left) {
      |               ^
a.cc:47:1: error: 'Node' was not declared in this scope
   47 | Node * currNode = root - > left;
      | ^~~~
a.cc:47:8: error: 'currNode' was not declared in this scope
   47 | Node * currNode = root - > left;
      |        ^~~~~~~~
a.cc:47:26: error: expected primary-expression before '>' token
   47 | Node * currNode = root - > left;
      |                          ^
a.cc:48:19: error: expected primary-expression before '>' token
   48 | while (currNode - > right != NULL &&
      |                   ^
a.cc:49:12: error: expected primary-expression before '>' token
   49 | currNode - > right != root) {
      |            ^
a.cc:50:23: error: expected primary-expression before '>' token
   50 | currNode = currNode - > right;
      |                       ^
a.cc:52:16: error: expected primary-expression before '>' token
   52 | if (currNode - > right == NULL) {
      |                ^
a.cc:53:12: error: expected primary-expression before '>' token
   53 | currNode - > right = root;
      |            ^
a.cc:54:15: error: expected primary-expression before '>' token
   54 | root = root - > left;
      |               ^
a.cc:56:12: error: expected primary-expression before '>' token
   56 | currNode - > right = NULL;
      |            ^
a.cc:60:23: error: expected primary-expression before '>' token
   60 | cout << " " << root - > id;
      |                       ^
a.cc:60:25: error: 'id' was not declared in this scope; did you mean 'void'?
   60 | cout << " " << root - > id;
      |                         ^~
      |                         void
a.cc:61:15: error: expected primary-expression before '>' token
   61 | root = root - > right;
      |               ^
a.cc: In member function 'void Solution::reverse(int*, int*)':
a.cc:65:1: error: 'Node' was not declared in this scope
   65 | Node * a = NULL, * b = from, * c = from - > right;
      | ^~~~
a.cc:65:8: error: 'a' was not declared in this scope
   65 | Node * a = NULL, * b = from, * c = from - > right;
      |        ^
a.cc:65:20: error: 'b' was not declared in this scope
   65 | Node * a = NULL, * b = from, * c = from - > right;
      |                    ^
a.cc:65:32: error: 'c' was not declared in this scope
   65 | Node * a = NULL, * b = from, * c = from - > right;
      |                                ^
a.cc:65:43: error: expected primary-expression before '>' token
   65 | Node * a = NULL, * b = from, * c = from - > right;
      |                                           ^
a.cc:67:5: error: expected primary-expression before '>' token
   67 | b - > right = a;
      |     ^
a.cc:71:9: error: expected primary-expression before '>' token
   71 | c = c - > right;
      |         ^
a.cc: In member function 'void Solution::postorder(int*)':
a.cc:76:1: error: 'Node' was not declared in this scope
   76 | Node * forgeRoot = new Node(-1);
      | ^~~~
a.cc:76:8: error: 'forgeRoot' was not declared in this scope
   76 | Node * forgeRoot = new Node(-1);
      |        ^~~~~~~~~
a.cc:76:24: error: expected type-specifier before 'Node'
   76 | Node * forgeRoot = new Node(-1);
      |                        ^~~~
a.cc:77:13: error: expected primary-expression before '>' token
   77 | forgeRoot - > left = root;
      |             ^
a.cc:80:15: error: expected primary-expression before '>' token
   80 | while (root - > left) {
      |               ^
a.cc:81:8: error: 'currNode' was not declared in this scope
   81 | Node * currNode = root - > left;
      |        ^~~~~~~~
a.cc:81:26: error: expected primary-expression before '>' token
   81 | Node * currNode = root - > left;
      |                          ^
a.cc:82:19: error: expected primary-expression before '>' token
   82 | while (currNode - > right != NULL &&
      |                   ^
a.cc:83:12: error: expected primary-expression before '>' token
   83 | currNode - > right != root) {
      |            ^
a.cc:84:23: error: expected primary-expression before '>' token
   84 | currNode = currNode - > right;
      |                       ^
a.cc:86:16: error: expected primary-expression before '>' token
   86 | if (currNode - > right == NULL) {
      |                ^
a.cc:87:12: error: expected primary-expression before '>' token
   87 | currNode - > right = root;
      |            ^
a.cc:88:15: error: expected primary-expression before '>' token
   88 | root = root - > left;
      |               ^
a.cc:90:12: error: expected primary-expression before '>' token
   90 | currNode - > right = NULL;
      |            ^
a.cc:91:15: error: expected primary-expression before '>' token
   91 | root = root - > right;
      |               ^
a.cc:94:8: error: 'from' was not declared in this scope
   94 | Node * from = root - > right - > left, * to = root;
      |        ^~~~
a.cc:94:22: error: expected primary-expression before '>' token
   94 | Node * from = root - > right - > left, * to = root;
      |                      ^
a.cc:94:32: error: expected primary-expression before '>' token
   94 | Node * from = root - > right - > left, * to = root;
      |                                ^
a.cc:94:42: error: 'to' was not declared in this scope; did you mean 'tm'?
   94 | Node * from = root - > right - > left, * to = root;
      |                                          ^~
      |                                          tm
a.cc:97:23: error: expected primary-expression before '>' token
   97 | cout << " " << from - > id;
      |                       ^
a.cc:97:25: error: 'id' was not declared in this scope; did you mean 'void'?
   97 | cout << " " << from - > id;
      |                         ^~
      |                         void
a.cc:98:15: error: expected primary-expression before '>' token
   98 | from = from - > right;
      |               ^
a.cc:100:23: error: expected primary-expression before '>' token
  100 | cout << " " << from - > id;
      |                       ^
a.cc:100:25: error: 'id' was not declared in this scope; did you mean 'void'?
  100 | cout << " " << from - > id;