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 ...
随机推荐
- ESXI6.0新添加硬盘未能格式化成功
最近练手,手头现有的硬盘是从其他机器上拆下来的,插入ESXI主机上,然后在系统配置硬盘的时候,不能格式化 报错 提示如下错误:"在ESXi"xxx.xxx.xxx.xxx" ...
- ArcGIS API for Javascript之专题图的制作(四)热力图渲染(上)
一 .热力图定义 热力图(heat map)也称热图,是以特殊颜色高亮区域的形式表示密度.温度.气压.频率等分布的不易理解和表达的数据. 二.HeatmapRenderer esri/renderer ...
- BZOJ3632:外太空旅行(最大团,DFS)
Description 在人类的触角伸向银河系的边缘之际,普通人上太空旅行已经变得稀松平常了.某理科试验班有n个人,现在班主任要从中选出尽量多的人去参加一次太空旅行活动. 可是n名同学并不是和平相处的 ...
- java通过反射调用有参数的方法
public static void eachCfg(Class Initclass,String taskType){ Field[] fields = Initclass.getDeclaredF ...
- OpenCV (C++) 几何形状识别(面积过滤、横纵比过滤等等)
#include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespac ...
- DDOS攻击详解
导读 Ddos的攻击方式有很多种,最基本的Dos攻击就是利用合理的服务请求来占用过多的服务资源,从而使合法用户无法得到服务的响应. 在信息安全的三要素——“保密性”.“完整性”和“可用性”中,DoS( ...
- $\rm{NOIp}$板子整理
怎么说呢,整理这个的目的就是为了有个简约的\(list\),方便以后查阅,复习起来不至于太吃力. 并且--好像重温一遍所有,会更有一些新的认识.这也算是对我所学的一点整理了吧. 一.并查集的两种方式 ...
- Android 在测试阶段当出现多个测试服务器地址时打包的小技巧
前提:服务端没有做特殊处理 在开发android网络客户端项目时,不可避免的会用到“测试服务器地址”和“云端服务器地址”等.(有时可能会有多个) 这时在打包给测试那帮哥们时,你就需要一个服务器地址打上 ...
- 微信小程序开发 [01] 小程序基本结构和官方IDE简介
1.小程序账户注册 实际上在进行开发时没有注册小程序账户也是可以的,官方提供的IDE提供实时编译模拟预览,和当前你有没有绑定小程序账户没有关系. 当然,最终你要正式上线你的小程序的话,肯定还是需要账户 ...
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...