C++/代码:
class Solution { public: vector<vector<int>> Print(TreeNode* pRoot) { vector<vector<int>> res; //定义二维输出数组 if (!pRoot) return res; queue<TreeNode*> q; q.push(pRoot); q.push(NULL); //插入结束标识符 vector<int> level; while (q.size()) { auto t = q.front(); q.pop(); if (!t) { if (level.empty()) break; res.push_back(level); level.clear(); q.push(NULL); //插入结束标识符 continue; } level.push_back(t->val); if (t->left) q.push(t->left); if (t->right) q.push(t->right); } return res; } };