Advertisement
bogolyubskiyalexey

fill brothers

May 8th, 2020
1,310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. void addBrother(node&* begin, node&* last, node* brother) {
  2.     if (brother) {
  3.         if (begin) {
  4.             last->brother = brother;
  5.             last = brother;
  6.         } else {
  7.             begin = last = brother;
  8.         }
  9.     }
  10. }
  11.  
  12. void bfs(node* node) {
  13.     while(node) {
  14.         node* brothersBegin = nullptr;
  15.         node* brothersLast = nullptr;
  16.         for (node* item = node; item; item = item->brother) {
  17.             addBrother(brothersBegin, brothersLast, item->left);
  18.             addBrother(brothersBegin, brothersLast, item->right);
  19.         }
  20.         node = brothersBegin;
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement