Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. Tree

Convert Sorted Array to Binary Search Tree

PreviousHouse Robber IIINextRestore Binary Tree

Last updated 2 years ago

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

A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.

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:

Idea

Binary divide + recursion

Since left subtree need to be smaller than root and right subtree need to be larger than root, use left half and right half with regard to the root (mid index) to fill the tree

For example, mid index value = 0, -10 & 3 need to be in left sub tree and 5 & 9 need to be in right sub tree. Therefore, fill left of root 0 by picking the mid index value from [-10,-3] (-10) and fill right of root 0 with mid index value from [5,9] (9)

Then fill right of root -10 with -3 and fill left of root 9 with 5

Code

public TreeNode sortedArrayToBST(int[] nums) {
    return fill(0, nums.length-1, nums);
}    
public TreeNode fill(int low, int high, int[] nums){
    if(low>high){
        return null;
    }
    int mid = (low+high)/2;
    TreeNode root = new TreeNode(nums[mid]);
    if(low==high){
        return root;
    }
    root.left = fill(low, mid-1, nums);
    root.right = fill(mid+1, high, nums);
    return root;
}