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) 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的更多相关文章
- 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 ...
- 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 ...
- 【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. ...
- LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height
C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...
- [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 ...
- 【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 ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- extundelete数据恢复
需要安装的依赖包: 1. e2fsprogs软件包已安装2. e2fsprogs-libs软件包已安装3. e2fsprogs-devel软件包已安装4. gcc软件包已安装5. gcc-c++ 软件 ...
- asp.net中的CheckBox控件的使用
CheckBox控件中的最重要属性就是checked属性了 下面就是使用checked属性的一个小应用; 先建立一个wed窗体:在窗体中写下这些代码: <%@ Page Language=&qu ...
- js DateTime函数
---恢复内容开始--- 一.js获取当前日期时间var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFull ...
- springcloud第四步:ribbon搭建服务负载均衡
使用ribbon实现负载均衡 启动两个会员服务工程,端口号分别为8762.8763,订单服务 使用负载均衡策略轮训到会员服务接口. 什么是ribbon ribbon是一个负载均衡客户端 类似nginx ...
- 2019春第十周作业Compile Summarize
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 在这里 我在这个课程的目标是 能够对C语言的编写更加得心应手 这个作业在那个具体方面帮助我实现目标 结构体更进一步 参考文献与网址 C语言 ...
- xgboost 最优参数, df某一个字段进行字符串搜索
0.909323 with: {'max_depth': 6, 'min_child_weight': 0.8, 'n_estimators': 800} df_huoguo = df[df.c ...
- Cocos Creator 生命周期回调(官方文档摘录)
Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...
- ABP 2.0.2 升到 2.2.1
1.选择解决方案 右键 管理 nuget 更新 输入abp 这里只升级 abp的包 点升级 2.update-database 可能需要你添加个迁移(这一步可能不需要) 3.Core 项目下面的Au ...
- Git 教程(四):标签和其他
标签管理 发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库 ...
- you've successfully authenticated, but Gitee.com does not provide she access.
如果都是正常的生成ssh的操作,还是会报这个错误,那么就是.... 你没更改文件夹的权限,这个坑跳了很久(汗...) sudo chmod 777 -r 文件夹