Build a Binary Tree from Descriptions

Quick Overview

Construct a binary tree from parent-child-side descriptions and return its root, following the cited tree-construction problem.

Build a Binary Tree from Descriptions

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Build a Binary Tree from Descriptions Implement `build_tree_level_order(descriptions: list[list[int]]) -> list[int]`. Each description is `[parent, child, is_left]`, where `is_left` is `1` when `child` is the left child and `0` when it is the right child. The input describes one valid binary tree with distinct positive node values. Build the tree and return its level-order serialization. Use `-1` for a missing child position that occurs before a later real node, and remove all trailing `-1` values. This serialization fixes the output order and represents the constructed shape exactly. ## Valid Input Domain - Every child has exactly one parent. - A parent has at most one left and one right child. - Exactly one node is never listed as a child; it is the root. ## Constraints - `1 <= descriptions.length <= 100,000` - `1 <= parent, child <= 1,000,000,000` ## Public Examples ### Example 1 Input: `[[20, 15, 1], [20, 17, 0]]` Output: `[20, 15, 17]` ### Example 2 Input: `[[1, 2, 0], [2, 3, 1]]` Output: `[1, -1, 2, 3]` ```hint Identify the root independently Record child links while also tracking which node values have appeared as children. ```

Quick Answer: Construct a binary tree from parent-child-side descriptions and return its root, following the cited tree-construction problem.

|Home/Coding & Algorithms/ByteDance
ByteDance logo
ByteDance
Aug 28, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Build a Binary Tree from Descriptions

Implement build_tree_level_order(descriptions: list[list[int]]) -> list[int].

Each description is [parent, child, is_left], where is_left is 1 when child is the left child and 0 when it is the right child. The input describes one valid binary tree with distinct positive node values.

Build the tree and return its level-order serialization. Use -1 for a missing child position that occurs before a later real node, and remove all trailing -1 values. This serialization fixes the output order and represents the constructed shape exactly.

Valid Input Domain

  • Every child has exactly one parent.
  • A parent has at most one left and one right child.
  • Exactly one node is never listed as a child; it is the root.

Constraints

  • 1 <= descriptions.length <= 100,000
  • 1 <= parent, child <= 1,000,000,000

Public Examples

Example 1

Input: [[20, 15, 1], [20, 17, 0]]

Output: [20, 15, 17]

Example 2

Input: [[1, 2, 0], [2, 3, 1]]

Output: [1, -1, 2, 3]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...