Monday, February 25, 2013
tree bfs
void FindNode(Node* root)
{
deque<Node*> queue1,queue2;
queue1.push_back(root);
int level=0;
Node* last_node=root;
while(!queue1.empty() || !queue2.empty())
{
if(!queue1.empty())
{
Node* cur_node=queue1.front();
last_node=cur_node;
if(cur_node->L)
queue2.push_back(cur_node->L);
else if(cur_node->R)
queue2.push_back(cur_node->R);
queue1.pop_front();
}
if(!queue2.empty())
{
Node* cur_node=queue2.front();
last_node=cur_node;
if(cur_node->L)
queue1.push_back(cur_node->L);
else if(cur_node->R)
queue1.push_back(cur_node->R);
queue2.pop_front();
}
level++;
}
cout<<last_node->val<<endl;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment