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 ...
随机推荐
- idea 2017破解方法
http://blog.csdn.net/zx110503/article/details/78734428
- wget 的 使用方法
问题: 最近在使用 wget ,感觉有很多的功能都不会,现在进行写一篇文章,更新一些wget的使用技巧,防止以后忘记的时候,重新回来进行查阅. 正文: 现在经常使用: curl -O url 下载文件 ...
- seq 序列
seq 序列用法: seq [option]...last seq [option]...first last seq [option]...first increment last 例如:seq 5 ...
- 项目管理心经——正确的使用Microsoft Project
第一步:填写task items 第二步:定义个性化日历(Tools-->Change working time) 第二步:填写task duration 第三步:填写前置任务 第四步:自动生成 ...
- cJSON 使用详解
由于c语言中,没有直接的字典,字符串数组等数据结构,所以要借助结构体定义,处理json.如果有对应的数据结构就方便一些, 如python中用json.loads(json)就把json字符串转变为内建 ...
- MongoDB3.2配置文件.md
Core Options systemLog Options systemLog: verbosity: <int> quiet: <boolean> traceAllExce ...
- header头
<?php header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在 heade ...
- Date对象的用法总结
var dt=new Date(); var dt=+new Date();//一种特殊的写法,只适用于这个Date的对象 console.log(dt); dt.getFullYear();//年 ...
- leetcode62—Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- scrapy (三)各部分意义及框架示意图详解
一.框架示意图 Scrapy由 Python 编写,是一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试 ...