<p>You are given a 2D integer array <code>descriptions</code> where <code>descriptions[i] = [parent<sub>i</sub>, child<sub>i</sub>, isLeft<sub>i</sub>]</code> indicates that <code>parent<sub>i</sub></code> is the <strong>parent</strong> of <code>child<sub>i</sub></code> in a <strong>binary</strong> tree of <strong>unique</strong> values. Furthermore,</p>

<ul>
	<li>If <code>isLeft<sub>i</sub> == 1</code>, then <code>child<sub>i</sub></code> is the left child of <code>parent<sub>i</sub></code>.</li>
	<li>If <code>isLeft<sub>i</sub> == 0</code>, then <code>child<sub>i</sub></code> is the right child of <code>parent<sub>i</sub></code>.</li>
</ul>

<p>Construct the binary tree described by <code>descriptions</code> and return <em>its <strong>root</strong></em>.</p>

<p>The test cases will be generated such that the binary tree is <strong>valid</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/09/example1drawio.png" style="width: 300px; height: 236px;" />
<pre>
<strong>Input:</strong> descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]
<strong>Output:</strong> [50,20,80,15,17,19]
<strong>Explanation:</strong> The root node is the node with value 50 since it has no parent.
The resulting binary tree is shown in the diagram.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/09/example2drawio.png" style="width: 131px; height: 300px;" />
<pre>
<strong>Input:</strong> descriptions = [[1,2,1],[2,3,0],[3,4,1]]
<strong>Output:</strong> [1,2,null,null,3,4]
<strong>Explanation:</strong> The root node is the node with value 1 since it has no parent.
The resulting binary tree is shown in the diagram.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= descriptions.length &lt;= 10<sup>4</sup></code></li>
	<li><code>descriptions[i].length == 3</code></li>
	<li><code>1 &lt;= parent<sub>i</sub>, child<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= isLeft<sub>i</sub> &lt;= 1</code></li>
	<li>The binary tree described by <code>descriptions</code> is valid.</li>
</ul>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
        unordered_map<int, TreeNode*> mp;
        int size= descriptions.size();
        set<int> allEntry;
        for(int i=0;i<size;i++){
            int parent=descriptions[i][0], child=descriptions[i][1], isLeft=descriptions[i][2];
            allEntry.insert(parent);
            mp[parent]=mp[parent]?mp[parent]: new TreeNode(parent);
            mp[child]=mp[child]?mp[child]: new TreeNode(child);
            if(isLeft) mp[parent]->left=mp[child];
            else mp[parent]->right=mp[child];
        }
        for(int i=0;i<size;i++) allEntry.erase(descriptions[i][1]);
        return mp[*allEntry.begin()];  
    }
    // Solution after watching NeetcodeIo youtube solution
/*
    TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
        unordered_map<int, TreeNode*> mp;
        int size= descriptions.size();
        set<int> allEntry;
        for(int i=0;i<size;i++){
            int parent=descriptions[i][0], child=descriptions[i][1], isLeft=descriptions[i][2];
            allEntry.insert(parent);
            if(mp.find(parent)==mp.end()) mp[parent]= new TreeNode(parent);
            if(mp.find(child)==mp.end()) mp[child]= new TreeNode(child);
            if(isLeft) mp[parent]->left=mp[child];
            else mp[parent]->right=mp[child];
        }
        for(int i=0;i<size;i++) allEntry.erase(descriptions[i][1]);
        return mp[*allEntry.begin()];  
    }
*/
};



// Unoptimised solution
class Solution_first_try{
private:
    int findParent(vector<vector<int>>& descriptions){
        int size = descriptions.size();
        unordered_map<int,int> allEntry;

        //filling up the allEntry map
        for(int i=0;i<size;i++){
            if(allEntry[descriptions[i][0]]!=-1) allEntry[descriptions[i][0]]=1;
            allEntry[descriptions[i][1]]=-1;
        }

        // find parent
        for(auto it:allEntry) if(it.second==1) return it.first;
        return -1;
    }
public:
    TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
        int size = descriptions.size();
        unordered_map<int,pair<int,int>> parentMap;

        //find parents
        int parent=findParent(descriptions);

        // fill all the maps
        for(int i=0;i<size;i++){
            pair<int,int> p;
            if(parentMap.find(descriptions[i][0])!=parentMap.end()){
                p=parentMap[descriptions[i][0]];
                if(descriptions[i][2]) p.first=descriptions[i][1];
                else p.second=descriptions[i][1];
            }else p=descriptions[i][2]?make_pair(descriptions[i][1],-1):make_pair(-1,descriptions[i][1]);
            parentMap[descriptions[i][0]]=p;
        }

        // filling up the tree
        TreeNode* root = new TreeNode(parent);
        stack<int> st;
        unordered_map<int,TreeNode*> mp;
        st.push(parent);
        mp[parent]=root;
        while(!st.empty()){
            int currentNode=st.top();
            st.pop();
            TreeNode* t = mp[currentNode];
            if(parentMap.find(currentNode)!=parentMap.end()){
                TreeNode* left, right;
                pair<int,int> child=parentMap[currentNode];
                if(child.first!=-1){
                    t->left = new TreeNode(child.first);
                    mp[child.first]= t->left;
                    st.push(child.first);
                }
                if(child.second!=-1){
                    t->right = new TreeNode(child.second);
                    mp[child.second] = t->right;
                    st.push(child.second);
                }
            }
        }
        return root;
    }
};
275015