LeetCode-Easy刷题(23) Convert Sorted Array to Binary Search Tree
Posted 当以乐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Easy刷题(23) Convert Sorted Array to Binary Search Tree相关的知识,希望对你有一定的参考价值。
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.将有序数组转化为二分查找树
public TreeNode sortedArrayToBST(int[] nums)
return toBSTHelp(nums, 0, nums.length-1);
public TreeNode toBSTHelp(int[] nums,int start,int end)
if(start>end)
return null;
int mid = (start+end)/2;
TreeNode root = new TreeNode(nums[mid]);
root.left = toBSTHelp(nums, start, mid-1);
root.right= toBSTHelp(nums, mid+1, end);
return root;
以上是关于LeetCode-Easy刷题(23) Convert Sorted Array to Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode-Easy刷题(31) Single Number