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.

Example

Example 1:
Input: {1,2}
Output: A binary search tree with minimal height. Explanation:
2
/
1 Example 2:
Input: {1,2,3,4,5,6,7}
Output: A binary search tree with minimal height. Explanation: 4
/ \
2 6
/ \ / \
1 3 5 7

Notice

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

思路:

Binary Search Tree特性:Binary Search Tree中序遍历的结果是升序数组。对于一个升序数组,一旦确定了根节点,根节点左半边部分全部属于左子树,根节点右半部分全部属于右子树。

最小高度的二叉树,就要尽可能满足其平衡。也就是说尽量保证根节点的左子树和右子树的节点个数差不多。所以一开始把数组中间的那个数定为根节点。

一旦确定了根节点在数组中的index值,其左子树的范围则是A[start, index - 1],右子树的范围则是A[index + 1, end], 然后用分治法递归,求出根的左子树和右子树

代码:

/**
* 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) { if (A.length == 0 || A == null) {
return null;
}
return helper(A, 0, A.length - 1);
}
public TreeNode helper(int[] A, int start, int end) {
if (start > end) {
return null;
}
if (start == end) {
return new TreeNode(A[start]);
}
int mid = (start + end) / 2;
TreeNode root = new TreeNode(A[mid]);
root.left = helper(A, start, mid - 1);
root.right = helper(A, mid + 1, end);
return root;
}
}
 

Lintcode177-Convert Sorted Array to Binary Search Tree With Minimal Height-Easy的更多相关文章

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

  2. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

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

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

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

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

  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. [Unity][安卓]Unity和Android Studio 3.0 交互通讯(1)Android Studio 3.0 设置

    [安卓]Android Studio 3.0 JDK安卓环境配置(2017.10) http://blog.csdn.net/bulademian/article/details/78387052 [ ...

  2. centos7的systemd命令对比

    centos7的systemd命令对比 http://www.linuxidc.com/Linux/2014-09/106490p2.htmhttp://www.linuxidc.com/Linux/ ...

  3. Linux主机之间ssh免密登录配置方法

    由于公司的生产环境有很多台Linux的CentOS服务器, 为了方便机子(假设两台机子A,B)互相之间免密ssh, scp命令操作,配置如下 1. 在A.B上分别创建本机的公钥和私钥,输入命令后连续三 ...

  4. 基于Servlet的MVC模式用户登录实例

    关于MVC模式的简单解释 M Model,模型层,例如登录实例中,用于处理登录操作的类: V View,视图层,用于展示以及与用户交互.使用html.js.css.jsp.jQuery等前端技术实现: ...

  5. B. Lynyrd Skynyrd

    传送门: 题意:给出 n,m,q 然后给出模板串,从1-n数字只出现一次,然后给出长度为m的要询问的串. q组询问:每组询问输出 ‘1’或者‘0’ 每组询问 一对x,y    问在x到y中有没有模板串 ...

  6. git加速和只下载部分目录

    浅复制 工作要用到的.git有1.8G太大了.下载过程要好几个小时,太慢了.可以这样操作 git clone 默认会下载项目的完整历史版本,如果你只关心最新版的代码,而不关心之前的历史信息,可以使用 ...

  7. 基数排序-LSD

    这个最低位优先的基数排序,非常适合移植到硬件设备中,所以,我们提供一个C源码 —————————————————————————————————————— #include <stdio.h&g ...

  8. Check SQL Server Deadlock

    Sometimes a script keeps running for a long time and can't stop, then a db blocking is occurring. We ...

  9. HDU 2196树形DP(2个方向)

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  10. PHP----------php的opcache扩展配置参数介绍

    [opcache]zend_extension = "路径/ext/php_opcache.dll" ; Zend Optimizer + 的开关, 关闭时代码不再优化.opcac ...