2196.cpp (632B)
1 class Solution { 2 public: 3 TreeNode *createBinaryTree(const vector<vector<int>> &descriptions) { 4 vector<bool> child(100001, false); 5 TreeNode *um[100001] = {0}; 6 7 for (const auto &desc : descriptions) { 8 if (!um[desc[0]]) um[desc[0]] = new TreeNode(desc[0]); 9 if (!um[desc[1]]) um[desc[1]] = new TreeNode(desc[1]); 10 (desc[2] ? um[desc[0]]->left : um[desc[0]]->right) = um[desc[1]]; 11 child[desc[1]] = true; 12 } 13 14 for (const auto &desc : descriptions) { 15 if (!child[desc[0]]) return um[desc[0]]; 16 } 17 18 return nullptr; 19 } 20 };