精華區beta Marginalman 關於我們 聯絡資訊
2196. Create Binary Tree From Descriptions 做完看了一下這邊其他人做的 感覺都差不多 不過一開始以為那個description長這樣: {parent, left, right} 結果居然是: {parent, child, isLeft} 神摸鬼 --- class Solution { public: TreeNode* getNodePtr(int node, unordered_map<int, TreeNode*> &map) { if (!map.count(node)) { map[node] = new TreeNode(node); } return map[node]; } TreeNode* createBinaryTree(vector<vector<int>>& descriptions) { unordered_map<int, TreeNode*> nodeMap; unordered_set<int> childs, parents; for (auto d: descriptions) { TreeNode* parentNode = getNodePtr(d[0], nodeMap); if (d[2]) parentNode->left = getNodePtr(d[1], nodeMap); else parentNode->right = getNodePtr(d[1], nodeMap); childs.insert(d[1]); parents.insert(d[0]); } for (auto p: parents) { if (!childs.count(p)) { return nodeMap[p]; } } return nullptr; } }; -- 噗尼都會的鍊金術...... 如、如果是這個的話,我說不定也能學會! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 72.190.48.202 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1721085364.A.C7A.html