java.lang.Object
g2101_2200.s2196_create_binary_tree_from_descriptions.Solution

public class Solution extends Object
2196 - Create Binary Tree From Descriptions.<p>Medium</p> <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><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/02/09/example1drawio.png" alt="" /></p> <p><strong>Input:</strong> descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]</p> <p><strong>Output:</strong> [50,20,80,15,17,19]</p> <p><strong>Explanation:</strong> The root node is the node with value 50 since it has no parent.</p> <p>The resulting binary tree is shown in the diagram.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/02/09/example2drawio.png" alt="" /></p> <p><strong>Input:</strong> descriptions = [[1,2,1],[2,3,0],[3,4,1]]</p> <p><strong>Output:</strong> [1,2,null,null,3,4]</p> <p><strong>Explanation:</strong> The root node is the node with value 1 since it has no parent.</p> <p>The resulting binary tree is shown in the diagram.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= descriptions.length <= 10<sup>4</sup></code></li> <li><code>descriptions[i].length == 3</code></li> <li><code>1 <= parent<sub>i</sub>, child<sub>i</sub> <= 10<sup>5</sup></code></li> <li><code>0 <= isLeft<sub>i</sub> <= 1</code></li> <li>The binary tree described by <code>descriptions</code> is valid.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • createBinaryTree

      public TreeNode createBinaryTree(int[][] descriptions)