You are given a 2D integer array descriptions where descriptions[i] = [parenti, childi, isLefti] indicates that parenti is the parent of childi in a binary tree of unique values. Furthermore,


	If isLefti == 1, then childi is the left child of parenti.
	If isLefti == 0, then childi is the right child of parenti.


Construct the binary tree described by descriptions and return its root.

The test cases will be generated such that the binary tree is valid.

&nbsp;
Example 1:

Input: descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]
Output: [50,20,80,15,17,19]
Explanation: The root node is the node with value 50 since it has no parent.
The resulting binary tree is shown in the diagram.


Example 2:

Input: descriptions = [[1,2,1],[2,3,0],[3,4,1]]
Output: [1,2,null,null,3,4]
Explanation: The root node is the node with value 1 since it has no parent.
The resulting binary tree is shown in the diagram.


&nbsp;
Constraints:


	1 &lt;= descriptions.length &lt;= 104
	descriptions[i].length == 3
	1 &lt;= parenti, childi &lt;= 105
	0 &lt;= isLefti &lt;= 1
	The binary tree described by descriptions is valid.


class Solution:
    def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
        hashmap = {}
        nodes = set()
        children = set()
        for parent,child,isLeft in descriptions:
            nodes.add(parent)
            nodes.add(child)
            children.add(child)
            if parent not in hashmap:
                hashmap[parent] = TreeNode(parent)
            if child not in hashmap:
                hashmap[child] = TreeNode(child)
            if isLeft:
                hashmap[parent].left = hashmap[child]
            if not isLeft:
                hashmap[parent].right = hashmap[child]
        
        for node in nodes:
            if node not in children:
                return hashmap[node]
class Solution {
    public TreeNode createBinaryTree(int[][] descriptions) {
        HashMap<Integer,TreeNode> map=new HashMap<>();
        HashSet<Integer> children=new HashSet<>();
        for(int[] info:descriptions)
        {
            int parent=info[0],child=info[1];
            boolean isLeft=info[2]==1?true:false;
            TreeNode parentNode=null;
            TreeNode childNode=null;
            if(map.containsKey(parent))
                parentNode=map.get(parent);
            else
                parentNode=new TreeNode(parent);
            if(map.containsKey(child))
                childNode=map.get(child);
            else
                childNode=new TreeNode(child);
            if(isLeft)
                parentNode.left=childNode;
            else
                parentNode.right=childNode;
            map.put(parent,parentNode);
            map.put(child,childNode);
            children.add(child);
            
        }
        TreeNode root=null;
        for(int info[]:descriptions)
        {
            if(!children.contains(info[0]))
            {
                root=map.get(info[0]);
                break;
            }
        }
        return root;
    }
    
    
}
class Solution {
public:
    TreeNode* createBinaryTree(vector<vector<int>>& descriptions){
        unordered_map<int, TreeNode*> getNode; //to check if node alredy exist
        unordered_map<int, bool> isChild; //to check if node has parent or not
        for(auto &v: descriptions){
            if(getNode.count(v[0])==0){
                TreeNode* par = new TreeNode(v[0]);
                getNode[v[0]] = par;
            }
            if(getNode.count(v[1])==0){
                TreeNode* child = new TreeNode(v[1]);
                getNode[v[1]] = child;
            }
            if(v[2]==1) getNode[v[0]]->left = getNode[v[1]]; //left-child
            else getNode[v[0]]->right = getNode[v[1]]; //right-child
            isChild[v[1]] = true;
        }
        TreeNode* ans = NULL;
        for(auto &v: descriptions){
            if(isChild[v[0]] != true){ //if node has no parent then this is root node
                ans = getNode[v[0]];
                break;
            }
        }
        return ans;
    }
};
var createBinaryTree = function(descriptions) {
  let nodes = new Map(), children = new Set();
  for (let [parent, child, isLeft] of descriptions) {
    let parentNode = nodes.get(parent) || new TreeNode(parent);
    if (!nodes.has(parent)) nodes.set(parent, parentNode);

    let childNode = nodes.get(child) || new TreeNode(child);
    if (!nodes.has(child)) nodes.set(child, childNode);

    if (isLeft) parentNode.left = childNode;
    else parentNode.right = childNode;

    children.add(child);
  }

  for (let [parent, child, isLeft] of descriptions) {
    // a node with no parent is the root
    if (!children.has(parent)) return nodes.get(parent);
  }
};
Create Binary Tree From Descriptions