Jun 24, 2009

Write the code to print a Binary tree level by level STARTING FROM THE LEAF LEVEL and then give exhaustive test cases.
eg: 1
2 3
4 5 6 7
Output: 4567231
queue+stack
deque (which can add or remove node from both ends)

bool foo(tree* root)
{
if(!root) return false;
queue q;
stack s;
q.push(root);
while(!q.empty())
{
tree* tmp=q.front();
q.pop();
if(tmp->right)
q.push(tmp->right);
if(tmp->left)
q.push(tmp->left);
s.push(tmp);
}
while(!s.empty())
{
cout<val;
s.pop():
}
return true;
}

No comments:

Post a Comment