32-2分行从上到下打印二叉树

class Solution {
public:
        vector > Print(TreeNode* pRoot) {
            vector>ans;
            if(!pRoot)return ans;
            queueq;
            q.push(pRoot);
            while(!q.empty())
            {
                int size=q.size();
                vectortemp;
                while(size--)
                {
                    TreeNode*top=q.front();
                    q.pop();
                    temp.push_back(top->val);
                    if(top->left)q.push(top->left);
                    if(top->right)q.push(top->right);
                }
                ans.push_back(temp);
            }
            return ans;
        }
    
};

你可能感兴趣的:(32-2分行从上到下打印二叉树)