刷题记录第三十二天-二叉树的层序遍历

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> que;
        que.push(root);
        vector<vector<int>> result;
        while(!que.empty()){
            int n=que.size();
            vector<int> num{};
            for(int i=0;i<n;i++){
                TreeNode* current=que.front();
                que.pop();
                if(current!=nullptr){
                    num.push_back(current->val);
                    que.push(current->left);
                    que.push(current->right);
                }	
            }
            if(num.size()!=0)
                result.push_back(num);
        }
        return result;
    }
};

你可能感兴趣的:(每日一题,算法,数据结构)