| 350 | } |
| 351 | |
| 352 | vector<node*> levelwiselinkedlist(binarytree<int>*root) |
| 353 | { |
| 354 | node *head=NULL; |
| 355 | node *tail=NULL; |
| 356 | vector<node*>output; |
| 357 | queue<binarytree<int>*>pendingnodes; |
| 358 | pendingnodes.push(root); |
| 359 | pendingnodes.push(NULL); |
| 360 | |
| 361 | while(pendingnodes.size()!=0) |
| 362 | { |
| 363 | binarytree<int>*front=pendingnodes.front(); |
| 364 | pendingnodes.pop(); |
| 365 | |
| 366 | if(pendingnodes.size()==0) |
| 367 | { |
| 368 | output.push_back(head); |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | if(front==NULL) |
| 373 | { |
| 374 | output.push_back(head); |
| 375 | head=NULL; |
| 376 | tail=NULL; |
| 377 | pendingnodes.push(NULL); |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | node *p=new node; |
| 382 | p->data=front->data; |
| 383 | p->next=NULL; |
| 384 | |
| 385 | if(head==NULL) |
| 386 | { |
| 387 | head=tail=p; |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | tail->next=p; |
| 392 | tail=p; |
| 393 | } |
| 394 | |
| 395 | if(front->left) |
| 396 | pendingnodes.push(front->left); |
| 397 | |
| 398 | if(front->right) |
| 399 | pendingnodes.push(front->right); |
| 400 | |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return output; |
| 405 | } |
| 406 | |
| 407 | void displaylinkedlist(node * p) |
| 408 | { |