Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

Solution:

 /**
* 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.length==0)
return null; int end = num.length-1;
TreeNode root = sortedArrayToBSTRecur(num,0,end);
return root;
} public TreeNode sortedArrayToBSTRecur(int[] num, int head, int end){
if (head==end){
TreeNode root = new TreeNode(num[head]);
return root;
} if (head+1==end){
TreeNode root = new TreeNode(num[end]);
TreeNode child = new TreeNode(num[head]);
root.left = child;
return root;
} //Calculate the median index.
int len = end-head;
int mid = head + len/2 + len%2;
TreeNode root = new TreeNode(num[mid]);
TreeNode leftChild = sortedArrayToBSTRecur(num,head,mid-1);
TreeNode rightChild = sortedArrayToBSTRecur(num,mid+1,end);
root.left = leftChild;
root.right = rightChild;
return root;
}
}

Leetcode-Convert Sorted Array to BST的更多相关文章

  1. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  2. LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  3. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  4. LeetCode - Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  5. LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]

    1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...

  6. LeetCode——Convert Sorted Array to Binary Search Tree

    Description: Given an array where elements are sorted in ascending order, convert it to a height bal ...

  7. LeetCode Convert Sorted Array to Binary Search Tree(数据结构)

    题意: 将一个有序的数组建成一棵平衡的BST树. 思路: 因为数组已经有序,每次可以从中点开始建根,再递归下去分别处理左/右子树. /** * Definition for a binary tree ...

  8. [leetcode]Convert Sorted Array to Binary Search Tree @ Python

    原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉 ...

  9. [leetcode]Convert Sorted Array to Binary Search Tre

    排好序的... 中间是root , root左边是left,root右边是right 递归建树. /** * Definition for binary tree * struct TreeNode ...

  10. LeetCode 108. Convert Sorted Array to Binary Search Tree (将有序数组转换成BST)

    108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...

随机推荐

  1. [Java基础]List,Map集合总结

    java.util包下: Collection    |--List 接口 |----ArrayList |----LinkedList |----Vector |-----Stack |---Set ...

  2. vue TypeError: Cannot read property 'length' of undefined 问题解决思路

    上图中是我在接手一个项目的时候,出现的一个关于数组的 bug,但是从上图来看,其实报错是不清楚的,只能从报错信息上看出来是 /src/page/waybill/waybill_form.vue 这个 ...

  3. ajax实现json循环输出结果

    $.post("bankInfo.php",{key:jee_server,uid:jee_uid},function(data) { var strs=JSON.stringif ...

  4. 为什么 Objective-C 很难

    转自:http://www.oschina.net/question/213217_41058 作为一个Objective-C的coder,我总能听到一部分人在这门语言上抱怨有很多问题.他们总在想快速 ...

  5. iOS开发多线程篇 04 —线程间的通信

    iOS开发多线程篇—线程间的通信 一.简单说明 线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个线程传递数据给另1个线程 在1个线程中执行完特定任 ...

  6. CentOS6.2下Qt5.1.0无法输入中文

    因为在程序中需要在界面上输入中文,但是系统是英文系统,没有预装中文输入法,于是从网上搜了一下输入法的安装,但是输入法安装好之后,可以再系统中输入中文,但是却无法再Qt中输入中文,只能继续找解决办法 安 ...

  7. FileStream常用的属性和方法 (转)

    对流进行操作时要引用 using System.IO; 命名空间 FileStream常用的属性和方法: 属性: CanRead 判断当前流是否支持读取,返回bool值,True表示可以读取 CanW ...

  8. CentOs 7 安装总结

    1.安装 前几天安装了CentOs 7系统,弄了好几次都进不去图形安装界面,总是进入 "dracut:/# _".本以为是系统盘没做好,就用了另外几种工具重做了盘,还是一样.后来在 ...

  9. jsp tld 的tag 自定义标签扩展

    引入方式 <%@ taglib prefix="bgt" uri="/WEB-INF/tlds/bgt.tld" %> 写法示例如下: <?x ...

  10. 7-16 Bestcoder a Oracle

    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...