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 ...
随机推荐
- 图解Storm
问题导读:1.你认为什么图形可以显示hadoop与storm的区别?(电梯)2.本文是如何形象讲解hadoop与storm的?(离线批量处理.实时流式处理)3.hadoop map/reduce对应s ...
- 最近发现docker感觉不错
最近发现docker感觉不错,接下来开始学习docker方面的技术.lxc也可以学学. storm,kafka也要熟悉起来.
- DOM的概念及子节点类型
前言 DOM的作用是将网页转为一个javascript对象,从而可以使用javascript对网页进行各种操作(比如增删内容).浏览器会根据DOM模型,将HTML文档解析成一系列的节点,再由这些节点组 ...
- 警惕自己,不断学习c++【转】
每天早上起床看一遍,时刻警惕自己,每天至少要浏览http://www.cplusplus.com 1.把C++当成一门新的语言学习(和C没啥关系!真的.):2.看<Thinking In C++ ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- jquery实现隐藏,简化和更多
HTML代码: <div class="box"> <div class="header"> <h3>图书分类</h3 ...
- [BZOJ2788][Poi2012]Festival
2788: [Poi2012]Festival Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 187 Solved: 91[Submit][Statu ...
- Hibernate条件查询
设计上可以灵活的根据 Criteria 的特点来方便地进行查询条件的组装.现在对 Hibernate的Criteria 的用法进行总结:Hibernate 设计了 CriteriaSpecificat ...
- continue 语句
停止循环的当前迭代,并开始新的迭代. continue [label]; 可选的 label 参数指定 continue 应用于哪条语句. 说明 只能在 while.do...while.for.或 ...
- 制作、解析带logo的二维码
用DecoderQRCode来解析带logo的二维码,发现报错,解析不了,于是便又查资料,找到了更强大的制作二维码 工具:GooleZXing 首先下GooleZXing的jar包. -------- ...