Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (franchouchouISBEST)時間1年前 (2024/04/16 22:55), 1年前編輯推噓4(400)
留言4則, 4人參與, 1年前最新討論串125/1548 (看更多)
623. Add One Row to Tree 肥肥用BFS 用recursive的都是觸手吧 剩肥肥我又臭又長了 TreeNode* addOneRow(TreeNode* root, int val, int depth) { queue<pair<TreeNode*,int>> q; q.push({root, 1}); while(!q.empty()) { TreeNode* cur = q.front().first; int cur_level = q.front().second; q.pop(); if(cur_level == depth-1) { TreeNode* node_left = new TreeNode(val); TreeNode* node_right = new TreeNode(val); node_left->left = cur->left; node_right->right = cur->right; cur->left = node_left; cur->right = node_right; } else { if(cur->left) q.push({cur->left, cur_level+1}); if(cur->right) q.push({cur->right, cur_level+1}); } } if(depth == 1) { TreeNode* new_root = new TreeNode(val, root, NULL); return new_root; } return root; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.138.94.21 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1713279324.A.AB3.html

04/16 22:57, 1年前 , 1F
大師
04/16 22:57, 1F
※ 編輯: DJYOSHITAKA (223.138.94.21 臺灣), 04/16/2024 22:57:27

04/16 23:00, 1年前 , 2F
大師
04/16 23:00, 2F

04/16 23:14, 1年前 , 3F
大師
04/16 23:14, 3F

04/16 23:29, 1年前 , 4F
大師
04/16 23:29, 4F
文章代碼(AID): #1c7f5Sgp (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1c7f5Sgp (Marginalman)