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 ...
随机推荐
- hadoop 流streaming跑python程序
先放上命令: hadoop jar /usr/hadoop-/contrib/streaming/hadoop-streaming-.jar -mapper mapper.py -file mappe ...
- Mac下android_sdk配置环境变量
1.启动Terminal终端工具 2.输入cd ~/ 进入当前用户的home目录 3. 创建: touch .bash_profile 4.打开并编辑: open .bash_profile 5.在文 ...
- Android & iOS 第三方 Crash ANR 捕捉上传
1. Bugly 地址:http://bugly.qq.com/ 提供:专业的Crash(崩溃).Android ANR(application not response).iOS卡顿监控和解决方案. ...
- 应用程序间跳转 (友盟SSO 授权 与系统自带的分享)
应用程序间跳转的应用场景 使用第三方用户登录,如微信登录,返回用户名和密码 需要用户授权,返回到调用程序,同时返回授权的用户名 应用程序推广,跳转到itunes并显示指定app下载页 第三方支付,跳转 ...
- BZOJ4127: Abs
Description 给定一棵树,设计数据结构支持以下操作 1 u v d 表示将路径 (u,v) 加d 2 u v 表示询问路径 (u,v) 上点权绝对值的和 Input 第一行两个整数n和m,表 ...
- C语言工具---Code::Blocks
Code::Blocks Code::Blocks 是一个开源的全功能的跨平台C/C++集成开发环境. Code::Blocks是开放源码软件.由纯粹的C++语言开发完成,它使用了著名的图形界面库wx ...
- PHP 下的SSL加密设置
这个是报的错[Composer\Downloader\TransportException] The . OpenSSL Error messages: error::SSL routines:SSL ...
- Linux-CentOS 6.5 mini 中没有curses.h的问题
1.直接贴过程 [fengbo@CentOS: jigsaw]$ rpm -q ncursesncurses-5.7-3.20090208.el6.i686[fengbo@CentOS: jigsaw ...
- 使用C#实现FTP的文件上传和下载【转】
参考博文:http://blog.163.com/mity_rui@126/blog/static/1098136182013101525615577/
- Picture effect of JavaScript
每隔一定时间跟换图片Img = new Array("image/2.jpg","image/1.jpg","image/3.jpg",&q ...