vaibhav1906

Untitled

Jul 14th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. class Solution {
  2. public:
  3. vector<vector<int>> levelOrder(TreeNode* root) {
  4. if(root==NULL){
  5. return {};
  6. }
  7. queue<TreeNode*> q;
  8. q.push(root);
  9. vector<vector<int>> v;
  10. v.push_back({root->val});
  11. while(q.size()!=0){
  12. int n = q.size();
  13. vector<int> s;
  14. for(int i=0; i<n; i++){
  15. if(q.front()->left!=NULL){
  16. q.push(q.front()->left);
  17. s.push_back(q.front()->left->val);
  18. }
  19. if(q.front()->right!=NULL){
  20. q.push(q.front()->right);
  21. s.push_back(q.front()->right->val);
  22. }
  23. q.pop();
  24. }
  25. v.push_back(s);
  26. }
  27. v.erase(v.end()-1);
  28. return v;
  29.  
  30. }
  31. };
Add Comment
Please, Sign In to add comment