GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — EdwardLeetcode/ConvertSortedArraytoBinarySearchTree.java at master · shua2018ti/EdwardLeetcode

Expand file tree

/

ConvertSortedArraytoBinarySearchTree.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
35 lines (31 loc) · 919 Bytes
/

ConvertSortedArraytoBinarySearchTree.java

Copy path

File metadata and controls

35 lines (31 loc) · 919 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package leetcode;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : ConvertSortedArraytoBinarySearchTree
* Creator : Edward
* Date : Sep, 2017
* Description : TODO
*/
public class ConvertSortedArraytoBinarySearchTree {
/**
* 108. Convert Sorted Array to Binary Search Tree
[1,2,3,4,5]
time : O(n);
space : O(n);
* @param nums
* @return
*/
public TreeNode sortedArrayToBST(int[] nums) {
if (nums == null || nums.length == 0) return null;
return helper(nums, 0, nums.length - 1);
}
public TreeNode helper(int[] nums, int left, int right) { // space : O(logn);
if (left > right) return null;
int mid = (right - left) / 2 + left;
TreeNode node = new TreeNode(nums[mid]);
node.left = helper(nums, left, mid - 1);
node.right = helper(nums, mid + 1, right);
return node;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.