Re: [閒聊] 每日leetcode
看板Marginalman作者enmeitiryous (enmeitiryous)時間1年前 (2024/08/26 08:26)推噓1(1推 0噓 1→)留言2則, 2人參與討論串764/1548 (看更多)
題目:
590. N-ary Tree Postorder Traversal
給你一顆n-ary tree(child<=n),回傳他的postorder travel vector
思路:昨天是一般binary tree的post order,今天是n-ary tree,由於給定node下的
child在vector中本來就是由左到右排列,所以對其依序遍歷再塞進ans中即可
void mygo(Node* root,vector<int>& pre_ans){
if(root==nullptr){
return;
}
for(auto k:root->children){
mygo(k,pre_ans);
}
pre_ans.push_back(root->val);
}
vector<int> postorder(Node* root) {
vector<int> ans;
mygo(root,ans);
return ans;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.227.216.18 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1724631969.A.828.html
→
08/26 08:27,
1年前
, 1F
08/26 08:27, 1F
推
08/26 08:38,
1年前
, 2F
08/26 08:38, 2F
討論串 (同標題文章)
完整討論串 (本文為第 764 之 1548 篇):