Build the Unique Binary Tree from Parent Descriptions

Quick Overview

Build the Unique Binary Tree from Parent Descriptions. Each description [parent,child,isLeft] specifies one edge in a valid binary tree with unique values: isLeft=1 means a left child and zero a right child. Construct the tree and return its actual TreeNode* root.

Build the Unique Binary Tree from Parent Descriptions

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each row `[parent, child, isLeft]` describes a parent-child edge in one valid binary tree whose node values are unique. If `isLeft` is `1`, the child is on the left; if it is `0`, the child is on the right. Construct the described tree and return its `TreeNode*` root. Descriptions may be supplied in any order. ```cpp class Solution { public: TreeNode* createBinaryTree(vector<vector<int>>& descriptions); }; ``` ### Examples **Example 1:** ```text descriptions = [[4,2,1],[4,6,0],[2,1,0]] Output: root 4, left child 2, right child 6; node 2 has right child 1. Explanation: Value 4 is the only node with no parent. ``` **Example 2:** ```text descriptions = [[7,3,0]] Output: root 7 with right child 3. Explanation: The zero flag selects the right link. ``` ### Constraints - `1 <= descriptions.length <= 10^4`. - Every row has three entries; parent and child values lie in `[1,10^5]`. - isLeft is zero or one; descriptions form one valid binary tree. ### Clarifying Questions The descriptions guarantee one valid binary tree. A binary tree need not be a binary search tree; the isLeft flag determines each child side. ```hint Inspect the structure Which mentioned value never appears as a child? ``` ### Reasoning checkpoints - Represent each distinct value by one node and satisfy every specified left or right connection. - Return the root of the entire described tree, independent of input row order. - Use the validity guarantee without imposing a binary-search-tree ordering that the source does not require. ### Optional extensions How would you validate conflicting descriptions if the validity guarantee were removed?

Overview: Build the Unique Binary Tree from Parent Descriptions. Each description [parent,child,isLeft] specifies one edge in a valid binary tree with unique values: isLeft=1 means a left child and zero a right child. Construct the tree and return its actual TreeNode* root.

Amazon logo
Amazon
May 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each row [parent, child, isLeft] describes a parent-child edge in one valid binary tree whose node values are unique. If isLeft is 1, the child is on the left; if it is 0, the child is on the right. Construct the described tree and return its TreeNode* root. Descriptions may be supplied in any order.

class Solution {
public:
    TreeNode* createBinaryTree(vector<vector<int>>& descriptions);
};

Examples

Example 1:

descriptions = [[4,2,1],[4,6,0],[2,1,0]]
Output: root 4, left child 2, right child 6; node 2 has right child 1.
Explanation: Value 4 is the only node with no parent.

Example 2:

descriptions = [[7,3,0]]
Output: root 7 with right child 3.
Explanation: The zero flag selects the right link.

Constraints

  • 1 <= descriptions.length <= 10^4 .
  • Every row has three entries; parent and child values lie in [1,10^5] .
  • isLeft is zero or one; descriptions form one valid binary tree.

Clarifying Questions Guidance

The descriptions guarantee one valid binary tree. A binary tree need not be a binary search tree; the isLeft flag determines each child side.

Reasoning checkpoints

  • Represent each distinct value by one node and satisfy every specified left or right connection.
  • Return the root of the entire described tree, independent of input row order.
  • Use the validity guarantee without imposing a binary-search-tree ordering that the source does not require.

Optional extensions

How would you validate conflicting descriptions if the validity guarantee were removed?