【leetcode】LCR150.彩灯装饰记录Ⅱ

时间:2024-11-21 07:48:12
#include<vector> using namespace std; #include<queue> #include<iostream> //* 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; queue<vector<TreeNode*>> nodeVecs; vector<TreeNode*> nodevec{root}; nodeVecs.push(nodevec); while (nodeVecs.empty()==false) { vector<TreeNode*> curNodeVec = nodeVecs.front(); cout<<"curNodeVec.size="<<curNodeVec.size()<<endl; nodeVecs.pop(); vector<int> ans_vec; // 当前结点的值 vector<TreeNode*> childNodes; // 当前结点的子结点 for(auto curNode : curNodeVec) { ans_vec.emplace_back(curNode->val); if(curNode->left!=nullptr) childNodes.emplace_back(curNode->left); if(curNode->right!=nullptr) childNodes.emplace_back(curNode->right); } if(childNodes.size()>0) nodeVecs.push(childNodes); if(ans_vec.size()>0) ans.emplace_back(ans_vec); } return ans; } }; int main() { return 0; }