Description

Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height.

There may exist multiple valid solutions, return any of them.

Example

Given [1,2,3,4,5,6,7], return

  1. 4
  2. / \
  3. 2 6
  4. / \ / \
  5. 1 3 5 7

解题:题目要求根据一个有序的数组创建一个二叉排序树,并且返回这个排序树。要求高度最小,那么必定是平衡二叉树,每次取有序序列最中间的那个数作为结点即可。递归方法,代码如下:

  1. /**
  2. * Definition of TreeNode:
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left, right;
  6. * public TreeNode(int val) {
  7. * this.val = val;
  8. * this.left = this.right = null;
  9. * }
  10. * }
  11. */
  12.  
  13. public class Solution {
  14. /*
  15. * @param A: an integer array
  16. * @return: A tree node
  17. */
  18. public TreeNode sortedArrayToBST(int[] A) {
  19. // write your code here
  20. return create(A, 0, A.length - 1);
  21. }
  22. private TreeNode create(int[]A, int first, int last){
  23. if(first >last)
  24. return null;
  25. int mid = (first + last) / 2;
  26. TreeNode node = new TreeNode( A[mid] );
  27. node.left = create(A, first, mid-1);
  28. node.right = create(A, mid+1, last);
  29. return node;
  30. }
  31. }

177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】的更多相关文章

  1. 【Lintcode】177.Convert Sorted Array to Binary Search Tree With Minimal Height

    题目: Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. ...

  2. Convert Sorted Array to Binary Search Tree With Minimal Height

    Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Exa ...

  3. LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height

    C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...

  4. Lintcode177-Convert Sorted Array to Binary Search Tree With Minimal Height-Easy

    177. Convert Sorted Array to Binary Search Tree With Minimal Height Given a sorted (increasing order ...

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

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

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

  8. 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree

    Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...

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

随机推荐

  1. ESXI6.0新添加硬盘未能格式化成功

    最近练手,手头现有的硬盘是从其他机器上拆下来的,插入ESXI主机上,然后在系统配置硬盘的时候,不能格式化 报错 提示如下错误:"在ESXi"xxx.xxx.xxx.xxx" ...

  2. ArcGIS API for Javascript之专题图的制作(四)热力图渲染(上)

    一 .热力图定义 热力图(heat map)也称热图,是以特殊颜色高亮区域的形式表示密度.温度.气压.频率等分布的不易理解和表达的数据. 二.HeatmapRenderer esri/renderer ...

  3. BZOJ3632:外太空旅行(最大团,DFS)

    Description 在人类的触角伸向银河系的边缘之际,普通人上太空旅行已经变得稀松平常了.某理科试验班有n个人,现在班主任要从中选出尽量多的人去参加一次太空旅行活动. 可是n名同学并不是和平相处的 ...

  4. java通过反射调用有参数的方法

    public static void eachCfg(Class Initclass,String taskType){ Field[] fields = Initclass.getDeclaredF ...

  5. OpenCV (C++) 几何形状识别(面积过滤、横纵比过滤等等)

    #include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespac ...

  6. DDOS攻击详解

    导读 Ddos的攻击方式有很多种,最基本的Dos攻击就是利用合理的服务请求来占用过多的服务资源,从而使合法用户无法得到服务的响应. 在信息安全的三要素——“保密性”.“完整性”和“可用性”中,DoS( ...

  7. $\rm{NOIp}$板子整理

    怎么说呢,整理这个的目的就是为了有个简约的\(list\),方便以后查阅,复习起来不至于太吃力. 并且--好像重温一遍所有,会更有一些新的认识.这也算是对我所学的一点整理了吧. 一.并查集的两种方式 ...

  8. Android 在测试阶段当出现多个测试服务器地址时打包的小技巧

    前提:服务端没有做特殊处理 在开发android网络客户端项目时,不可避免的会用到“测试服务器地址”和“云端服务器地址”等.(有时可能会有多个) 这时在打包给测试那帮哥们时,你就需要一个服务器地址打上 ...

  9. 微信小程序开发 [01] 小程序基本结构和官方IDE简介

    1.小程序账户注册 实际上在进行开发时没有注册小程序账户也是可以的,官方提供的IDE提供实时编译模拟预览,和当前你有没有绑定小程序账户没有关系. 当然,最终你要正式上线你的小程序的话,肯定还是需要账户 ...

  10. poj 2485 Highways (最小生成树)

    链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...