Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] num) {
if(num==null||num.length==0){
return null;
}
int length=num.length;
TreeNode treenode=sortedArrayToBST(num,0,length-1);//我每次总是忘记减一,以后要记住
return treenode ; } private TreeNode sortedArrayToBST(int[] num, int star, int tail) {
if(star>tail){
return null;
}
int mid=star+(tail-star+1)/2;
TreeNode root=new TreeNode(num[mid]);
root.left=sortedArrayToBST(num,star,mid-1);
root.right=sortedArrayToBST(num,mid+1,tail);
return root;
}
}
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.的更多相关文章
- Convert Sorted List to Balanced BST
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 【Lintcode】106.Convert Sorted List to Balanced BST
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- LeetCode Array Easy 88. Merge Sorted Array
Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...
- 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII
一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...
- [array] leetCode-4-Median of Two Sorted Arrays-Hard
leetCode-4-Median of Two Sorted Arrays-Hard descrition There are two sorted arrays nums1 and nums2 o ...
- Data Structure Array: Sort elements by frequency
http://www.geeksforgeeks.org/sort-elements-by-frequency-set-2/ #include <iostream> #include &l ...
随机推荐
- jquerymobile页面跳转和参数传递
http://blog.csdn.net/chen052210123/article/details/7481578 页面跳转: 页面跳转时pagebeforechange事件会被触发两次,通过$(d ...
- 经验分享:多屏复杂动画CSS技巧三则
当下CSS3应用已经相当广泛,其中重要成员之一就是CSS3动画.并且,随着CSS动画的逐渐深入与普及,更复杂与细腻的动画场景也如雨后春笋般破土而出.例如上个月做的「企业QQ-新年祝福」活动: 感谢sh ...
- Android 实用开源控件
图片放大缩小: PinchImageView 体验最好的图片手势控件,不同分辨率无缝切换,可与ViewPager结合使用. GestureViews 带有手势控制的ImageView和FrameLay ...
- XCODE UITextField 中的属性和用法
XCODE UITextField 中的属性和用法 一些基本的用法 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedR ...
- [备份]Emacs配置文件
(set-background-color "gray20")(set-foreground-color "wheat") (tool-bar-mode -1) ...
- 【HDU】2138 How many prime numbers
http://acm.hdu.edu.cn/showproblem.php?pid=2138 题意:给n个数判断有几个素数.(每个数<=2^32) #include <cstdio> ...
- UIButton在Disabled状态下标题混乱的问题
最近开发中遇到的问题汇总 有段时间没有归纳开发中遇到的一些问题了,今天就写一下之前开发中遇到的几个问题.希望这 篇文章能让读者在以后的开发中少走弯路.本文将依次介绍<UIButton在Disab ...
- curl测试网页响应时间
连接时间: curl -s -o /dev/null -w "%{time_connect}\n" http://www.joinpay.com 传输时间: curl -s -o ...
- CentOS7 网络管理相关命令
contos7 网卡配置文件自动识别ifcfg开头的文件(包括目录) CentOS7网卡命名规则: CentOS 7 开始对于网卡的编号则有另一套规则, 网卡的界面代号现在与网卡的来源有关,基本上的网 ...
- 利用call与apply向函数传递参数
Js中函数对象都有call与apply两个方法属性,二者使用方法和功能一样,只是传递参数的格式不同,call逐个传递单个参数,apply一次性传递一个参数数组. 这两个方法可以改变函数的调用对象,并且 ...