LeetCode107.二叉树的层次遍历II

题目

 1 class Solution {
 2 public:
 3     vector<vector<int>> levelOrderBottom(TreeNode* root) {
 4         auto levelOrder = vector<vector<int>> ();
 5         if(root == NULL ) 
 6             return levelOrder;
 7         queue<TreeNode*>q;
 8         q.push(root);
 9         while(!q.empty()){
10             int num = q.size();
11             auto level = vector<int> ();
12             while(num > 0){
13                 TreeNode* p = q.front();q.pop();level.push_back(p->val);
14                 if(p->left != NULL) q.push(p->left);
15                 if(p->right != NULL) q.push(p->right);
16                 num--;   
17             }
18             levelOrder.push_back(level);
19         }
20         reverse(levelOrder.begin(),levelOrder.end());
21         return levelOrder;
22     }
23 };

学习vector用法

posted @ 2021-01-02 16:34  Uitachi  阅读(62)  评论(0编辑  收藏  举报