<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.
# # class TreeNode:
# #     def __init__(self, val=0, left=None, right=None):
# #         self.val = val
# #         self.left = left
# #         self.right = right
# class Solution:
#     def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
#         '''
#         dict = {
#             node: [left, right, parent]
#         }

#         Key: 20, Value: [15, 17, 50]
#         Key: 15, Value: [None, None, 20]
#         Key: 17, Value: [None, None, 20]
#         Key: 50, Value: [20, 80, None]
#         Key: 80, Value: [19, None, 50]
#         Key: 19, Value: [None, None, 80]
#         '''
#         graph = collections.defaultdict(lambda: [None, None, None])

#         for des in descriptions:
#             print("des", des)
#             if des[2] == 0:
#                 graph[des[0]][1] = des[1]
#                 graph[des[1]][2] = des[0] 
#             else:
#                 graph[des[0]][0] = des[1]
#                 graph[des[1]][2] = des[0] 
        
#         root = None
#         for key, value in graph.items():
#             # print(f'Key: {key}, Value: {value}')
#             if value[2] == None:
#                 root = key
#                 break

#         root = TreeNode(root)
#         cur_root = root
#         q = collections.deque([cur_root.val])

#         while q:
#             cur_node = q.popleft()
#             cur_root = TreeNode(cur_node)
#             left, right, _ = graph[cur_node]
#             if left is not None:
#                 q.append(left)
#                 cur_root.left = left
#             else:
#                 cur_root.left = None
            
#             if right is not None:
#                 q.append(right)
#                 cur_root.right = right
#             else:
#                 cur_root.right = None
                    
#         return root

import collections
from typing import List, Optional

class Solution:
    def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
        graph = collections.defaultdict(lambda: [None, None, None])
        node_map = {}  # To hold TreeNode references

        # Build graph and initialize TreeNode references
        for des in descriptions:
            parent, child, is_left = des
            if is_left == 1:
                graph[parent][0] = child
            else:
                graph[parent][1] = child
            
            graph[child][2] = parent  # Assign parent

            if parent not in node_map:
                node_map[parent] = TreeNode(parent)
            if child not in node_map:
                node_map[child] = TreeNode(child)
        
        print(node_map)
        # Identify the root node
        root = None
        for key in node_map:
            if graph[key][2] is None:  # This node has no parent, thus it's the root
                root = node_map[key]
                break

        # Using a queue to set up the tree structure
        queue = collections.deque([root])

        while queue:
            current_root = queue.popleft()
            
            left_child_val = graph[current_root.val][0]
            right_child_val = graph[current_root.val][1]

            if left_child_val is not None:
                current_root.left = node_map[left_child_val]
                queue.append(current_root.left)
            
            if right_child_val is not None:
                current_root.right = node_map[right_child_val]
                queue.append(current_root.right)

        return root

1867