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 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】的更多相关文章
- 【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. ...
- 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 ...
- LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height
C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...
- 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 ...
- [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 ...
随机推荐
- C# 泛型约束 xxx Where T:约束(二)
泛型是什么? 通过上篇的实例 C# 泛型约束 xxx<T> Where T:约束(一),我们对泛型有一定的认识. 所谓泛型,即通过参数化类型来实现在同一份代码上操作多种数据类型,泛型编程 ...
- ZooKeeper 数据结构 & 命令
0. 说明 记录 ZooKeeper 数据结构 & 命令 1. ZooKeeper 数据结构 ZooKeeper 特性: ZooKeeper 文件系统以 / 为根目录,文件系统为树形结构,每 ...
- Qt: QAction在QToolBar中快捷键行为注意事项
在QMenuBar中添加快捷键很简单,只要在text的特定字母前加&,如&k按下ALT+k即会触发(QPushButton也是一样).但在QToolBar则不然,需要调action-& ...
- TiDB数据库 mydumper与loader导入数据
从mysql导出数据最好的方法是使用tidb官方的工具mydumper. 导入tidb最好的方法是使用loader工具,大概19.4G每小时的速度. 详细的步骤可以参考官网:https://pingc ...
- 关于MVC开发时,无法找到area的问题记录
解决方法: 检查area=admin 的dll是否生成,一般都是admin域生成dll导致
- Linux命令——用户和用户组管理
Linux命令--用户和用户组管理 命令groupadd 作用:新增组 格式:groupadd [-g GID] groupname 参数:-g,指定GID,一般从500开始 说明:一般不必加-g参数 ...
- DJI Mobile SDK 新教程
DJI Mobile SDK 新教程发布! http://bbs.dji.com/thread-20282-1-1.html Android 如何创建一个航拍相机App: 你将学到如何配置DJI Mo ...
- 使用Tortoise结合Git比较两个版本的差异
1.右键项目,TortoiseGit -------> Diff with previous version 2.单击出分支选择弹窗,进行选择要比较的两个分支 3.比较同个分支的两个不同的版本 ...
- nodejs 模板引擎jade的使用
1.test.jade文件 html head style body div.box div#div1 div aaa div(class="aaa left-warp active&quo ...
- 字典树Trie树
摘自大佬博客 https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 给出n个单词和m个询问1.查询某个前缀是否出现过2.查询某个单词是否出现过 ...