一、题目
. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/description/?envType=problem-list-v2&envId=binary-tree&difficulty=EASY&status=TO_DO
二、题解
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> decorateRecord(TreeNode* root) {
vector<vector<int>> ans;
if (root == nullptr) return ans;
TreeNode* q[1010];
int hh = 0, tt = -1;
q[++tt] = root;
while (hh <= tt) {
int sz = tt - hh + 1;
vector<int> row;
while (sz--) {
TreeNode* top = q[hh++];
row.push_back(top->val);
if (top->left) q[++tt] = top->left;
if (top->right) q[++tt] = top->right;
}
ans.push_back(row);
}
return ans;
}
};