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


题解:递归就可以了。

Java代码如下:

  1. /**
  2. * Definition for binary tree
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public TreeNode sortedArrayToBST(int[] num) {
  12. return Helper(num, 0, num.length-1);
  13. }
  14. public TreeNode Helper(int[] num,int begin,int end){
  15. if(begin > end)
  16. return null;
  17.  
  18. int mid = (begin + end)/2;
  19. TreeNode root = new TreeNode(num[mid]);
  20. root.left = Helper(num, begin, mid-1);
  21. root.right = Helper(num, mid+1, end);
  22. return root;
  23. }
  24. }

【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree的更多相关文章

  1. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

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

  2. LeetCode(108) Convert Sorted Array to Binary Search Tree

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

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

  4. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

  5. LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14

    108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...

  6. leetcode 108. Convert Sorted Array to Binary Search Tree 、109. Convert Sorted List to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 这个题使用二分查找,主要要注意边界条件. 如果left > right,就返回NULL.每次更新的 ...

  7. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

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

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

  9. 37. leetcode 108. Convert Sorted Array to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...

  10. [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)

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

随机推荐

  1. 【BZOJ】3315: [Usaco2013 Nov]Pogo-Cow(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3315 果然自己太弱. 想不出dp方程啊.. 其实,以后记住...与上一个状态或下一个状态有关,,可以 ...

  2. 【BZOJ】2020: [Usaco2010 Jan]Buying Feed, II (dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2020 和背包差不多 同样滚动数组 f[j]表示当前位置j份食物的最小价值 f[j]=min(f[j- ...

  3. 如何交换a,b的数值——一个简单的问题就证明现在的你依然弱爆了

    How? int c = a; a = b; b = c; 这样会浪费多一点内存去存放c,so还有吗? a ^= b; b ^= a; a ^= b; ^:异或. 答案碉堡了, 不过估计除了有可能在面 ...

  4. 图像增强:直方图均衡和小波变换【matlab】

    直方图均衡:统计图像像素灰度的直方图分布.对其进行重新分配以使图像的直方图分布更加均衡. 小波变换:图像轮廓主要体现在低频部分,可以通过对低频分解系数进行增强处理,对高频分解系数进行衰减处理,达到图像 ...

  5. VC++Debug查看堆对象内容,即使符号已经超出作用范围

    Sometimes you'd like to watch the value of an object (on the heap) even after the symbol goes of sco ...

  6. Windows 8.1 浏览器中 SkyDrive 的改名与隐藏

    在 Windows 8.1 中已经整合了 SkyDrive ,在中文版中 SkyDrive 的名字总是感觉不协调,可是在属性里面可以调整位置却不能修改名称,怎么办呢? 打开注册表,找到 HKEY_CL ...

  7. 52、图片缩放库 PhotoView

    PhotoView的简介: 这是一个图片查看库,实现图片浏览功能,支持pinch(捏合)手势或者点击放大缩小.支持在ViewPager中翻页浏览图片. PhotoView 是一款扩展自Android ...

  8. 程序运行时 0xC0000005: 读取位置 0x00000000 时发生访问冲突 ,可能是 com 组件引入各种问题

    在使用com组件事,可能引入很多不是问题的问题,比如CString 定义出运行时出错等等,这些问题解决的办法就是初始化组件 然后释放组件, 在使用组件时,如果仅仅用在按钮事件或者别的mfc 对话框类里 ...

  9. 翻页bug 在接口文档中应规范参数的取值区间

    <?php$a=array("red","green","blue","yellow","brown&q ...

  10. When an HTTP server receives a request for a CGI script

    cgicc: Overview of the Common Gateway Interface https://www.gnu.org/software/cgicc/doc/cgi_overview. ...