MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0108/Solution.java:14–33  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/09 desc :

Source from the content-addressed store, hash-verified

12 * </pre>
13 */
14public class Solution {
15 public TreeNode sortedArrayToBST(int[] nums) {
16 if (nums == null || nums.length == 0) return null;
17 return helper(nums, 0, nums.length - 1);
18 }
19
20 private TreeNode helper(int[] nums, int left, int right) {
21 if (left > right) return null;
22 int mid = (left + right) >>> 1;
23 TreeNode node = new TreeNode(nums[mid]);
24 node.left = helper(nums, left, mid - 1);
25 node.right = helper(nums, mid + 1, right);
26 return node;
27 }
28
29 public static void main(String[] args) {
30 Solution solution = new Solution();
31 TreeNode.print(solution.sortedArrayToBST(new int[]{0, 1, 2, 3, 4, 5, 6, 7}));
32 }
33}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected