LintCode:把排序数组转换为高度最小的二叉搜索树
来源:互联网 发布:mysql truncate table 编辑:程序博客网 时间:2024/05/01 07:24
给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树。
样例
给出数组 [1,2,3,4,5,6,7]
, 返回
4 / \ 2 6 / \ / \1 3 5 7
挑战
可能有多个答案,返回任意一个即可
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution{public TreeNode sortedArrayToBST(int[] num){if(num.length==0)return null;return sortedArrayToBST(num,0,num.length-1);}public TreeNode sortedArrayToBST(int[] num,int start,int end){if(start>end)return null;int mid=(start+end)/2;TreeNode root=new TreeNode(num[mid]);root.left=sortedArrayToBST(num,start,mid-1);root.right=sortedArrayToBST(num,mid+1,end);return root;}}
0 0
- LintCode SortedArrayToBST 把排序数组转换为高度最小的二叉搜索树
- lintcode-把排序数组转换为高度最小的二叉搜索树-177
- 把排序数组转换为高度最小的二叉搜索树(LintCode)
- LintCode:把排序数组转换为高度最小的二叉搜索树
- lintCode(177)——把排序数组转换为高度最小的二叉搜索树
- LintCode:把排序数组转换为高度最小的二叉搜索树
- lintcode,把排序数组转换为高度最小的二叉搜索树
- LintCode | 177. 把排序数组转换为高度最小的二叉搜索树
- LintCode 把排序数组转换为高度最小的二叉搜索树
- 把排序数组转换为高度最小的二叉搜索树-LintCode
- lintcode——把排序数组转换为高度最小的二叉搜索树
- lintcode把排序数组转换为高度最小的二叉搜索树
- LintCode-把排序数组转换为高度最小的二叉搜索树
- lintcode 把排序数组转换为高度最小的二叉搜索树
- LintCode 把排序数组转换为高度最小的二叉搜索树
- LintCode 177-把排序数组转换为高度最小的二叉搜索树
- 把排序数组转换为高度最小的二叉搜索树-LintCode
- [LintCode 177] 把排序数组转换为高度最小的二叉搜索树(Python)
- 关于scrolleView 的[MTMeViewController scrollViewDidScroll:]: message sent to deallocated instance 0x7fc
- android中handler用法总结
- 带宏函数等excel解析方法
- c#.net网页跳转七种方法
- 分享一种最简单的Android打渠道包的方法
- LintCode:把排序数组转换为高度最小的二叉搜索树
- iOS GitHub上值得留意的第三方库
- 分布式搜索Elasticsearch——概述
- 设置启动图片
- LeetCode 26:Remove Duplicates from Sorted Array
- iOS之自定义弹出框--AlertView
- 常用的正则表达式
- UIWebView设置字体大小,颜色,字体
- Python单元测试框架之Nose