Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

128. Convert Sorted Array to Binary Search Tree

EasyDay 38 of 60Trees & BST

You are given an integer array nums sorted in strictly increasing order. Build a height-balanced binary search tree that contains exactly these values and return its root. Height-balanced means that for every node, the heights of its two subtrees differ by at most 1. Any valid tree is accepted.

Example 1
Input: nums = [-10,-3,0,5,9]
Output: [0,-10,5,null,-3,null,9]
[0,-3,9,-10,null,5] is also accepted. Both are balanced BSTs.
Example 2
Input: nums = [1,3]
Output: [1,null,3]
[3,1] is also accepted.
Constraints
  • 1 <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4
  • nums is sorted in strictly increasing order
Hints
Frequently asked at: Amazon, Apple, Adobe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min