# PAT 1086. Tree Traversals Again Solution: DFS + Short Code

## Preface:

We use DFS to rebuild the whole tree by the inorder push pop sequence with simplified code implementation (pop could be ignored) and the use post order the traverse the whole tree.

## Tree Traversals Again Problem:

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.

Figure 1

**Input Specification:**

Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: “Push X” where X is the index of the node being pushed onto the stack; or “Pop” meaning to pop one node from the stack.

**Output Specification:**

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

**Sample Input:**

6 Push 1 Push 2 Push 3 Pop Pop Push 4 Pop Pop Push 5 Push 6 Pop Pop

**Sample Output:**

3 4 2 6 5 1

## Tree Traversals Again Solution Analysis:

The algorithm or approach is just using DFS to rebuild the whole tree by the inorder push pop sequence with simplified code implementation and the use post order the traverse the whole tree. The observation is that when we do “push val”, the subtree rooted at val is not built yet, so we keep recursively rebuild its left and right sub tree and then return, we could ignore pop operation actually, the following is the code which I consider it is already as short as possible:

struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int v) : val(v), left(NULL), right(NULL) {} ~TreeNode() {} }; std::string strOp; int itr = 0, val, n; bool flg; TreeNode *buildTree1() { if (itr >= n) return NULL; std::cin >> strOp; TreeNode *root = NULL; if ('u' == strOp[1]) { std::cin >> value; root = new TreeNode(value); ++itr, root->left = buildTree1(); ++itr, root->right = buildTree1(); } return root; } void dfs(TreeNode *root) { if (NULL == root) return; dfs(root->left); dfs(root->right); if (flg) { std::cout << root->val; flg = false; } else std::cout << ' ' << root->val; } int main() { std::cin >> n; n = (n << 1); TreeNode *root = buildTree(); flg = true; dfs(root); std::cout << std::endl; return 0; }

## Conclusion:

To summarize, we rebuild the whole tree first by the inorder push pop sequence recursively, (pop could be ignored actually) and then we use post order the traverse the whole tree.