/**
* 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.的更多相关文章

  1. Convert Sorted List to Balanced BST

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  2. 【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 ...

  3. 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 ...

  4. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  5. LeetCode Array Easy 88. Merge Sorted Array

    Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...

  6. 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 ...

  7. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

  8. [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 ...

  9. Data Structure Array: Sort elements by frequency

    http://www.geeksforgeeks.org/sort-elements-by-frequency-set-2/ #include <iostream> #include &l ...

随机推荐

  1. 【Xcelsius】在PPT中嵌入水晶易表Xcelsius2008仪表盘

    如果您使用Xcelsius创建了动画图形,并将其保存为 Shockwave® 文件(.swf 文件扩展名).但是往往插入进去之后,会产生一些比较棘手的问题,比如ppt不会自动播放,错误等等.今天把这些 ...

  2. ccc 旋转

    cc.Class({ extends: cc.Component, properties: { moveSpeed: 100, rotationSpeed: 90 }, // use this for ...

  3. unity 常用函数

    GameObject.FindGameObjectByTag(); anim.SetFloat("speed",Mathf.Abs(h)); Physics2D.lineCast2 ...

  4. BZOJ4607 : [PA2015 Final]Edycja

    显然做完操作$2$后再做操作$1$. 建立一个$26$个点的有向图,每个点只有一条出边,$i$->$j$表示$i$最终变成了$j$,边权为一开始是$i$,最后不是$j$的位置个数,如果$i\ne ...

  5. SDCycleScrollView 滚动视图的使用(广告)

    github库链接https://github.com/gsdios/SDCycleScrollView 无限循环自动图片轮播器(一步设置即可使用) // 网络加载图片的轮播器 cycleScroll ...

  6. flex lineChart 显示所有的数据节点

    .If you're using <mx:LineSeries>, then set the following property:itemRenderer="mx.charts ...

  7. ArrayList和Vector的扩容机制

    ArrayList和Vector都是继承了相同的父类和实现了相同的接口.如下 public class Vector<E> extends AbstractList<E> im ...

  8. 数据库之SQL语法

    -- 创建数据库CREATE DATABASE mytest; -- 创建表CREATE TABLE t_user( -- primary key 定义该列为主键列 -- AUTO_INCREMENT ...

  9. 远程访问mysql

    转载:http://www.codesky.net/article/201108/106005.html 数据库不允许从远程访问怎么办?本文提供了三种解决方法: 1.改表法.可能是你的帐号不允许从远程 ...

  10. [CareerCup] 17.11 Rand7 and Rand5 随机生成数字

    17.11 Implement a method rand7() given rand5(). That is, given a method that generates a random numb ...