Build Any Balanced BST from a Strictly Increasing Array

Quick Overview

Construct any height-balanced BST from a strictly increasing array without fixing a unique output shape.

Build Any Balanced BST from a Strictly Increasing Array

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Build a height-balanced binary search tree containing the integers in the strictly increasing array `nums`. Return its root. A tree is height-balanced when the heights of its two child subtrees differ by at most one at every node. The BST must contain each input value once, with smaller values in the left subtree and larger values in the right subtree. Any tree meeting these conditions is acceptable; no single root choice or serialized shape is required. ```cpp TreeNode* sortedArrayToBST(vector<int>& nums); ``` The supplied `TreeNode` has `val`, `left`, and `right` fields. Example brackets display nodes in level order and do not change the return type. ### Examples ```text Input: nums = [-6,-1,4,10] One valid output tree: [4,-1,10,-6] ``` Another valid tree is `[-1,-6,4,null,null,null,10]`. ```text Input: nums = [2,8] One valid output tree: [2,null,8] ``` The tree `[8,2]` is also valid. ### Constraints - The array length is between 1 and 10000. - Each value is between -10000 and 10000, inclusive. - Values are in strictly increasing order. ### What a Strong Answer Covers - A construction containing every strictly increasing input value once. - BST ordering and height balance at every node, with any valid output shape. - Node allocation, recursion depth, and cost without copying subarrays. ### Follow-up Questions - Which valid root choices are possible for two input values? - How do the required BST and balance properties constrain every subtree?

Overview: Construct any height-balanced BST from a strictly increasing array without fixing a unique output shape.

Amazon logo
Amazon
Date not reported
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Build a height-balanced binary search tree containing the integers in the strictly increasing array nums. Return its root. A tree is height-balanced when the heights of its two child subtrees differ by at most one at every node. The BST must contain each input value once, with smaller values in the left subtree and larger values in the right subtree.

Any tree meeting these conditions is acceptable; no single root choice or serialized shape is required.

TreeNode* sortedArrayToBST(vector<int>& nums);

The supplied TreeNode has val, left, and right fields. Example brackets display nodes in level order and do not change the return type.

Examples

Input: nums = [-6,-1,4,10]
One valid output tree: [4,-1,10,-6]

Another valid tree is [-1,-6,4,null,null,null,10].

Input: nums = [2,8]
One valid output tree: [2,null,8]

The tree [8,2] is also valid.

Constraints

  • The array length is between 1 and 10000.
  • Each value is between -10000 and 10000, inclusive.
  • Values are in strictly increasing order.

What a Strong Answer Covers Guidance

  • A construction containing every strictly increasing input value once.
  • BST ordering and height balance at every node, with any valid output shape.
  • Node allocation, recursion depth, and cost without copying subarrays.

Follow-up Questions Guidance

  • Which valid root choices are possible for two input values?
  • How do the required BST and balance properties constrain every subtree?