108. Convert Sorted Array to Binary Search Tree
Posted 张乐乐章
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.
1 class Solution { 2 public TreeNode sortedArrayToBST(int[] nums) { 3 return nums.length==0? null : ToBST(nums,0,nums.length-1); 4 5 6 } 7 public TreeNode ToBST(int[] nums,int lo,int hi) { 8 if(lo>hi) return null; 9 int mid = (lo+hi)/2; 10 TreeNode root = new TreeNode(nums[mid]); 11 root.left = ToBST(nums,lo,mid-1); 12 root.right = ToBST(nums,mid+1,hi); 13 14 return root; 15 16 } 17 }
以上是关于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