[剑指offer] 60. 把二叉树打印成多行

 

题目描述

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
class Solution
{
  public:
    vector<vector<int>> Print(TreeNode *pRoot)
    {
        vector<vector<int>> res;
        if (pRoot == NULL)
            return res;

        queue<TreeNode *> que;
        TreeNode *cur;
        que.push(pRoot);
        while (!que.empty())
        {
            vector<int> temp;
            int size = que.size();
            for (int i = 0; i < size; i++)
            {
                cur = que.front();
                que.pop();
                temp.push_back(cur->val);
                if (cur->left != NULL)
                    que.push(cur->left);
                if (cur->right != NULL)
                    que.push(cur->right);
            }
            res.push_back(temp);
        }
        return res;
    }
};

 

posted @ 2019-01-09 15:59  Ruohua3kou  阅读(112)  评论(0编辑  收藏  举报