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. 解决nohup中不写入日志的问题

    (一)问题描述: nohup 你的程序命令 如: nohup python manage.py runserver 0.0.0.0:6789   (此shell窗口1不要关,另外开一个shell窗口2 ...

  2. 关于 RESTful API 中 HTTP 状态码的定义

    最近正好使用了一会儿 Koa ,在这说一下自己对各个 请求码的见解和使用场景,懒人直接看 200.400.401.403.404.500 就可以了. 其中 2XX/3XX 其实都是请求成功,但是结果不 ...

  3. 2019年春季学期第四周作业Compile Summarize

    这个作业属于哪个课程 C语言程序设计一 这个作业要求在哪里 2019春季学期第四周作业 我的课程目标 重新学习有关数组的问题 这个作业在哪个具体方面帮助我实现目标 对于置换有了新的见解 参考文献 中国 ...

  4. 接口测试工具-poster

    1.发包工具-火狐浏览器插件 安装:打开火狐浏览器-alt键-工具-附加组件-搜索poster-点击安装-重启火狐浏览器-打开工具下的poster 2.使用 1)GET 1.输入URL 2.填入参数 ...

  5. 音乐类产品——“网易云音乐”app交互原型模板(免费使用)

    网易云音乐虽是一款音乐app,但有人说它也是社交界的一股清流以及一匹黑马.音乐带给人的感染,激发着很多人在这里表达着他们的情绪和心声.网易云音乐上的真实用户点评,不仅被印在地铁的广告牌上,还在朋友圈频 ...

  6. std::bind 的使用说明

    转自: https://www.cnblogs.com/cmranger/p/4743926.html ///////////////////// std::bind bind是对C++98标准中函数 ...

  7. 团队项目第一篇——NABCD

    团队名称: 团队项目名称: 团队口号: N(Need)需求: 现如今数据越来越零碎化,繁杂化,身为在校大学生的我们也因此对于时间的利用率也相应减少,为了时间的充分利用,减少在冗杂的信息中耽误的时间,充 ...

  8. Leetcode: Max Consecutive Ones II(unsolved locked problem)

    Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...

  9. c# ASP.NET Core2.2利用中间件支持跨域请求

    1.public void Configure(IApplicationBuilder app, IHostingEnvironment env)方法里面 不要加上:app.UseCors(); 2. ...

  10. pydemo_testMaopuSpider

    import json from multiprocessing import Pool import requests from requests.exceptions import Request ...