author: Blankj blog : http://blankj.com time : 2017/10/09 desc :
| 12 | * </pre> |
| 13 | */ |
| 14 | public 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected