Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. void count(Node *root) {
  2. static bool b = false;
  3. if(root == nullptr)
  4. {
  5. cout<<0;
  6. return;
  7. }
  8.  
  9. queue<Node*> q;
  10. q.push(root);
  11. cout<<1;
  12.  
  13. int nodesOnLevel = 1;
  14. int currentLevel = 0;
  15.  
  16. while(!q.empty())
  17. {
  18. Node* current = q.front();
  19. q.pop();
  20. nodesOnLevel--;
  21.  
  22. if(current->leftNode!=nullptr)
  23. {
  24. q.push(current->leftNode);
  25. }
  26. if(current->rightNode!=nullptr)
  27. {
  28. q.push(current->rightNode);
  29. }
  30.  
  31.  
  32. if(nodesOnLevel==0)
  33. {
  34. nodesOnLevel = q.size();
  35. if(nodesOnLevel!=0)
  36. { cout<<";"<<nodesOnLevel;
  37.  
  38. }
  39. currentLevel++;
  40.  
  41. }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement