From dda9b6d35cf554d1a2b422b76a3a675f483ae14c Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 15 Jul 2024 19:56:44 +0530 Subject: [PATCH] Create 2196. Create Binary Tree From Descriptions --- 2196. Create Binary Tree From Descriptions | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2196. Create Binary Tree From Descriptions 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; + + } +};