How to construct a binary tree from just the level order traversal string

Viewed 4858

Consider a binary tree with the following properties:

  1. An internal node (non-leaf node) has a value 1 if it has two children.
  2. A leaf node has a value 0 since it has no children.

A level order traversal on the tree would generate a string of 1s and 0s (by printing the weird value at each node as they are visited). Now given this string construct the binary tree and perform a post order traversal on the tree. The post order string should be the output of the program.

For example: Input String is 111001000. Create a binary tree from this. Then perform the post order traversal on the tree which would result in an output: 001001011

The "crux" of the problem is to create the binary tree from just the level order string. How would I do this?

6 Answers
Related