Jun 11, 2009

Write the code to print a Binary tree level by level STARTING FROM THELEAF LEVEL and then give exhaustive test cases.

Write the code to print a Binary tree level by level STARTING FROM THELEAF LEVEL and then give exhaustive test cases.

eg:
1
2 3
4 5 6 7
Output: 4567231

foo(tree* root)
{
queue q;
stack s;
q.push(root);
while(!q.empty())
{
tree* t=q.front();
q.pop();
s.push(t->val);
if(t->right)
q.push(t->right);
if(t->left)
q.push(t->left);
}
while(!s.empty())
{
int val=s.top();
print val;
s.pop();
}
}

No comments:

Post a Comment