/**
 * struct TreeNode {
 *  int val;
 *  struct TreeNode *left;
 *  struct TreeNode *right;
 * };
 */

class Solution {
  public:
    /**
     *
     * @param root TreeNode类
     * @return int整型vector<vector<>>
     */

    // 百度 l4 定位算法 一面 手撕 当时5分钟内没写出来 但有思路
    vector<vector<int> > levelOrder(TreeNode* root) {
        // write code here

        vector<vector<int>> ret;
        if (root == nullptr) {
            return ret;
        }

        queue<TreeNode*> qu;

        // 根结点入队
        qu.push(root);
        // ret.push_back({root->val});
        while (!qu.empty()) {
            int n = qu.size(); // 这一层节点数 从而控制 内部 存储的子数组大小  出队个数
            vector<int> ans;
            for (int i = 0; i < n; ++i) {
                TreeNode* tmp = qu.front();
                ans.push_back(tmp->val);
                qu.pop();
                if (tmp->left) {
                    qu.push(tmp->left);
                }

                if (tmp->right) {
                    qu.push(tmp->right);
                }
            }


            ret.push_back(ans);


        }

        return ret;


    }
};

方法一 广度优先 使用队列

每一层 填满当前队列,每次while都更新一层节点

百度错题,这次是看了提示 写对的 就差一个计数