108 Convert Sorted Array to Binary Search Tree 将有序数组转换为二叉搜索树
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
此题中,一个高度平衡二叉树是指一个二叉树每个节点的左右两个子树的高度差的绝对值不超过1。
示例:
给定有序数组: [-10,-3,0,5,9],
一种可行答案是:[0,-3,9,-10,null,5],它可以表示成下面这个高度平衡二叉搜索树:
0
/ \
-3 9
/ /
-10 5
详见:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/
Java实现:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode sortedArrayToBST(int[] nums) { return buildTree(nums,0,nums.length-1); } private TreeNode buildTree(int[] nums,int low,int high){ if(low>high){ return null; } int mid=(low+high)>>1; TreeNode node=new TreeNode(nums[mid]); node.left=buildTree(nums,low,mid-1); node.right=buildTree(nums,mid+1,high); return node; } }