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
stack
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<
s.pop():
}
return true;
}
No comments:
Post a Comment