leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

convert-sorted-array-to-binary-search-tree.dart (624B)


      1 //Take an input list called nums and convert it to a binary search tree.
      2 //The time complexity of this is O(n) where n is the number of values in the list.
      3 //Time: 257ms Beats: 71.43%
      4 //Memory: 143.6MB Beats: 57.14%
      5 class Solution {
      6   TreeNode? sortedArrayToBST(List<int> nums) {
      7     if(nums.isEmpty){
      8       return null;
      9     }
     10     TreeNode root = new TreeNode();
     11     int mid = (nums.length / 2).toInt();
     12     root.val = nums[mid];
     13     TreeNode? left = sortedArrayToBST(nums.sublist(0 , mid));
     14     TreeNode? right = sortedArrayToBST(nums.sublist(mid + 1));
     15     root.left = left;
     16     root.right = right;
     17     return root;
     18   }
     19 }