Re: [閒聊] 每日leetcode

看板Marginalman作者 (enmeitiryous)時間1年前 (2024/08/26 08:26), 編輯推噓1(101)
留言2則, 2人參與, 1年前最新討論串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
文章代碼(AID): #1coykXWe (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1coykXWe (Marginalman)