N本の交差木が与えられた場合、そのノード値の後方走査を返します。
再帰
class Solution{
public:
vector<int>postorder(Node*root){
vector<int>temp;
if(!root)return temp;
for(auto i:root->children){
postorder(i);
}
res.push_back(root->val);
return res;
}
};
反復
class Solution{
public:
vector<int>postorder(Node*root){
vector<int>res;
if(!root)
return res;
stack<Node*>stk;
stk.push(root)
while(!stk.empty()){
Node*temp=stk.top();
stk.pop();
res.push_back(temp->val);
for(auto i :temp->children){
stk.push(i);
}
}
reverse(res.begin(),res.end());
return res;
}
}