Suppose we are given a level order traversal output. How to construct a binary tree from that populated with the data at correct positions?
Please note that I’m not trying to sketch the tree from the given traversal output, but read off the traversal data from an array then populate a binary tree with it by actual coding in C.
Eg:
Let a[] = {A, B, C, D, E, F, G}; //the traversal output in an array
So level-order tree will look like this:
A
/ \
B C
/ \ / \
D E F G
Suppose there is a tree node structure like so:
typedef struct node
{
char data;
struct node* left;
struct node* right;
}tree;
Now I’m trying to read a[] values and code this tree so that it looks like the diagram. There are many examples of level-order traversal but couldn’t find anything relevant on actual coding for binary tree construction. This is sort of a “reverse of traversal”.
Also please note that this is not homework, though I don’t have problems tagging it if more people will notice it that way. 🙂
One possible solution:
stacktrace for the tree: