Skip to content

cmartinezal/Array-to-BST

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Convert Sorted Array to Binary Search Tree

Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.

Example 1:

Input: nums = [-10,-3,0,5,9]

Output: [0,-3,9,-10,null,5]

Explanation: [0,-10,5,null,-3,null,9] is also accepted

Example 2:

Input: nums = [1,3]

Output: [3,1]

Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.

Constraints:

  • 1 <= nums.length <= 10^4
  • 10^4 <= nums[i] <= 10^4
  • nums is sorted in a strictly increasing order.

Reference for problem description:

LeetCode: Convert Sorted Array to Binary Search Tree

Getting Started

Let's start to use this project.
There is a python file with the code and a Jupyter Notebook to visualize the code execution in your browser.

Prerequisites

Python 3 must be installed. Install requirements using requirements.txt file

pip3 install -r requirements.txt

Installation

To execute the project open the terminal and go to the project folder. Then execute it with python:

python3 array_to_BST.py

You can also use Jupyter Notebook to execute it in your browser or text editor directly.