c++实现
class Solution {
public:
using TreeNodeBool = pair<TreeNode*, bool>;
vector<vector<int>> decorateRecord(TreeNode* root)
{
vector<vector<int>> ans;
if(root==nullptr) return ans;
queue<vector<TreeNodeBool>> nodeVecs;
vector<TreeNodeBool> nodevec{{root,true}};
nodeVecs.push(nodevec);
while (nodeVecs.empty()==false)
{
vector<TreeNodeBool> curNodeVec = nodeVecs.front();
cout<<"curNodeVec.size="<<curNodeVec.size()<<endl;
nodeVecs.pop();
vector<int> ans_vec;
vector<TreeNodeBool> childNodes;
for(auto curNode : curNodeVec)
{
ans_vec.emplace_back(curNode.first->val);
if(curNode.first->left!=nullptr)
{
TreeNodeBool left{curNode.first->left, !curNode.second};
childNodes.emplace_back(left);
}
if(curNode.first->right!=nullptr)
{
TreeNodeBool right{curNode.first->right, !curNode.second};
childNodes.emplace_back(right);
}
}
if(curNodeVec[0].second==false && ans_vec.size()>0)
{
vector<int> ans_vec2;
for(auto it = ans_vec.rbegin(); it<ans_vec.rend(); ++it)
{
ans_vec2.emplace_back(*it);
}
ans.emplace_back(ans_vec2);
}
else if(ans_vec.size()>0) ans.emplace_back(ans_vec);
if(childNodes.size()>0) nodeVecs.push(childNodes);
}
return ans;
}
};