leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2196.cpp (632B)
0 class Solution { 1 public: 2 TreeNode *createBinaryTree(const vector<vector<int>> &descriptions) { 3 vector<bool> child(100001, false); 4 TreeNode *um[100001] = {0}; 5 6 for (const auto &desc : descriptions) { 7 if (!um[desc[0]]) um[desc[0]] = new TreeNode(desc[0]); 8 if (!um[desc[1]]) um[desc[1]] = new TreeNode(desc[1]); 9 (desc[2] ? um[desc[0]]->left : um[desc[0]]->right) = um[desc[1]]; 10 child[desc[1]] = true; 11 } 12 13 for (const auto &desc : descriptions) { 14 if (!child[desc[0]]) return um[desc[0]]; 15 } 16 17 return nullptr; 18 } 19 };