层序遍历模板



class Solution {
public:
    vector> levelOrder(Node* root) {
        queueque;
        vector>ans;

        if(root == nullptr) return ans;
        que.push(root);
        while(!que.empty()){
            int size = que.size();
            vectorres;
            while(size--){
                Node* node = que.front();
                que.pop();
                res.push_back(node->val);
                if(node->right) que.push(node->right);
                if(node->left) que.push(node->left);   
            }
            ans.push_back(res);
        }
        return ans;
    }
};

你可能感兴趣的:(刷题总结,算法,leetcode,c++)