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 ...
随机推荐
- [Python_6] Python 配置 MySQL 访问
0. 说明 Python 访问 MySQL 数据库,需要安装 MySQL 的 Python 插件. 1. 安装 MySQL 插件 pip install PyMySQL 2. 编写代码 # -*-co ...
- python3: 文件与IO
1.读写文本数据 # Write chunks of text data with open('somefile.txt', 'wt') as f: f.write(text1) # Redirect ...
- 深入了解MyBatis二级缓存
深入了解MyBatis二级缓存 标签: mybatis二级缓存 2015-03-30 08:57 41446人阅读 评论(13) 收藏 举报 分类: Mybatis(51) 版权声明:版权归博主所 ...
- 6.Solr4.10.3API使用(CURD)
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.在工程中引入solr-solrj-4.10.3.jar <dependency> <gr ...
- node学习笔记_01 环境搭建
一.下载安装nvm (node版本管理器),方便以后版本切换 nvm list -> 查看node版本(版本最好在8.0以上,不然在vsCode断点调试进不去,跟node版 ...
- Android学习之——自己搭建Http框架(1)
一.前言 近期学习http框架. 眼下写的这个框架临时仅仅适用于学习之用,实际用于项目之中还须要不断的优化. 要从server或者网络获取数据.显示到U ...
- bat替换文件的指定内容
需求:替换文件my.ini中的1000 为10000,bat脚本如下: c:cd C:\Program Files\MySQL\MySQL Server 5.5copy my.ini my1126ba ...
- Netty入门(四)ByteBuf 字节级别的操作
Netty 中使用 ByteBuf 代替 Java NIO 提供的 ByteBuffer 作为字节的容器. 一.索引 ByteBuf 提供两个指针变量支持读和写操作,读操作使用 readerInde ...
- php实现链表的基本操作
<?php class node{ private $value; private $next; public function __construct($value=0,$next=null) ...
- Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换(转载)
第二篇:JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换 1.函数介绍在Java中,字符串用统一的Unicode编码,每个字符占用两个字节,与编码有关的两 ...