diff --git a/2196. Create Binary Tree From Descriptions b/2196. Create Binary Tree From Descriptions new file mode 100644 index 0000000..1326869 --- /dev/null +++ b/2196. Create Binary Tree From Descriptions @@ -0,0 +1,45 @@ +class Solution { +public: + TreeNode* createBinaryTree(vector>& descriptions) { + + unordered_map mp; + unordered_set st; + + for(auto desc: descriptions){ + + int p = desc[0]; + int c = desc[1]; + int left = desc[2]; + + if(mp.find(p) == mp.end()){ + mp[p] = new TreeNode(p); + } + + if(mp.find(c) == mp.end()){ + mp[c] = new TreeNode(c); + } + + if(left == 1){ + mp[p]->left = mp[c]; + }else{ + mp[p]->right = mp[c]; + } + + st.insert(c); + + } + + for(auto desc: descriptions){ + + int p = desc[0]; + + if(st.find(p) == st.end()){ + return mp[p]; + } + + } + + return nullptr; + + } +};