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

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

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

 /**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/*
* @param A: an integer array
* @return: A tree node
*/
public TreeNode sortedArrayToBST(int[] A) {
// write your code here
return create(A, 0, A.length - 1);
}
private TreeNode create(int[]A, int first, int last){
if(first >last)
return null;
int mid = (first + last) / 2;
TreeNode node = new TreeNode( A[mid] );
node.left = create(A, first, mid-1);
node.right = create(A, mid+1, last);
return node;
}
}

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. mysql-client 与mysql-server的区别

    mysql-server 与 mysql-client是DBMS的两个面向不同操作对象的工具. server是DBMS面向物理层次,包含存储数据的一系列机制.处理方法的集成: client是DBMS面 ...

  2. glViewport()函数和glOrtho()函数的理解(转)

    http://www.cnblogs.com/yxnchinahlj/archive/2010/10/30/1865298.html 在OpenGL中有两个比较重要的投影变换函数,glViewport ...

  3. vue+axios自己踩过的坑

    axios的介绍就不用了吧,api有具体的介绍axios或者是axios中文: 主要讲的就是我自己在第一次使用axios中遇到的问题,及二次封装 先来说说二次封装,之前自己也是网上找了很多同学的封装, ...

  4. android studio InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClass annota

    如果 你的项目中使用了注解插件 比如butterknife   升级3.1之后打包编译  出现以下错误提示 InnerClass annotations are missing correspondi ...

  5. 漫画:高并发下的HashMap

    这一期我们来讲解高并发环境下,HashMap可能出现的致命问题. HashMap的容量是有限的.当经过多次元素插入,使得HashMap达到一定饱和度时,Key映射位置发生冲突的几率会逐渐提高. 这时候 ...

  6. CentOS 7的安装

    一.引导系统之后 界面说明: Install CentOS 7 安装CentOS 7 Test this media & install CentOS  7 测试安装文件并安装CentOS  ...

  7. Scala学习之路 (九)Scala的上界和下届

    一.泛型 1.泛型的介绍 泛型用于指定方法或类可以接受任意类型参数,参数在实际使用时才被确定,泛型可以有效地增强程序的适用性,使用泛型可以使得类或方法具有更强的通用性.泛型的典型应用场景是集合及集合中 ...

  8. 【转】 python中 * 的用法

    转自:https://www.cnblogs.com/jony7/p/8035376.html 1.表示乘号 2.表示倍数,例如: def T(msg,time=1):    print((msg+' ...

  9. Docker学习2-虚拟化

    虚拟化就是由位于下层的软件模块,根据上层的软件模块的期待,抽象(虚拟)出一个虚拟的软件或硬件模块,使上一层软件直接运行在这个与自己期待完全一致的虚拟环境上.从这个意义上来看,虚拟化既可以是软件层的抽象 ...

  10. (一)RESTful 介绍

    什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移”或“表现层状态转化”. R ...