i was solving a question from leetcode, where you have to create a binary tree from given description array. I am stuck on this from a long time and still can’t find the error. can somebody help me please?
class Solution {
public:
map<int,pair<TreeNode*,TreeNode*>> mp;
map<int,bool> isChild;
void solve(TreeNode* root)
{
if(!root)
return;
if(mp[root->val].first)
{
root->left=mp[root->val].first;
solve(root->left);
}
else
root->left=NULL;
if(mp[root->val].second)
{
root->right=mp[root->val].second;
solve(root->right);
}
else
root->right=NULL;
return;
}
TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
for(int i=0;i<descriptions.size();i++)
{
auto it=descriptions[i];
isChild[it[1]]=true;
TreeNode* node=new TreeNode(it[1]);
if(it[2])
mp[it[0]].first=node;
else
mp[it[0]].second=node;
}
TreeNode* root;
for(auto it:isChild)
{
if(it.second==false)
{
root=new TreeNode(it.first);
break;
}
}
solve(root);
return root;
}
};
Problem Link : - LeetCode