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.org.tw), 來自: 223.138.94.21 (臺灣) ※ 文章網址: https://ptt.org.tw/Marginalman/M.1713279324.A.AB3
wwndbk: 大師 04/16 22:57
argorok: 大師 04/16 23:00
SecondRun: 大師 04/16 23:14
Che31128: 大師 04/16 23:29