108. Convert Sorted Array to Binary Search Tree
Posted panini
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了108. Convert Sorted Array to Binary Search Tree相关的知识,希望对你有一定的参考价值。
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
2/17/2017, Java
performance不好
1 public class Solution { 2 public TreeNode sortedArrayToBST(int[] nums) { 3 if (nums == null) return null; 4 return sortedArrayToBST(nums, 0, nums.length - 1); 5 } 6 private TreeNode sortedArrayToBST(int[] nums, int lo, int hi) { 7 if (lo > hi) return null; 8 int mid = lo + (hi - lo) / 2; 9 TreeNode ret = new TreeNode(nums[mid]); 10 ret.left = sortedArrayToBST(nums, lo, mid - 1); 11 ret.right = sortedArrayToBST(nums, mid + 1, hi); 12 return ret; 13 } 14 }
以上是关于108. Convert Sorted Array to Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章
108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree
leetcode 108 Convert Sorted Array to Binary Search Tree