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 ...
随机推荐
- C语言扫盲及深化学习
c语言特点: (1)效率高 (2)控制性强 (3)硬件亲和性好 (4)可移植性高 一.关于注释 c语言中注释不能嵌套,因此注释代码时一定要注意源代码中是否已经存在注释.要从逻辑上删除一段代码,利用预编 ...
- Docker 私有仓库建立(加密和用户验证)
(一)生成证书1.mkdir /certs2.cd /certs 3.生成自签名证书 sudo openssl req -newkey rsa:2048 -new -nodes -x509 -days ...
- linux中巧用ctrl-z后台运行程序
我们知道后台运行任务时,可以用 () 和 &,但是有的时候我们会忘记加&,对于执行需要很长时间的任务来说,Ctrl+Z可能是一个比较好的把任务转入后台的方法. 当我们按下Ctrl+Z时 ...
- springmvc中的一些服务器报错
这是springmvc的前端控制器的加载格式要求.服务器加载web.xml后,它会在web-inf/下找名为spring[servlet名/handler处理器名]-servlet.xml文件. 可以 ...
- 利用反射和JDBC元数据实现更加通用的查询方法
package com.at221.jdbc; import java.io.IOException; import java.io.InputStream; import java.sql.*; i ...
- Python 写了个小程序,耗时一天,结果才100多行
from selenium import webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.co ...
- zabbix监控实战<3> 之自定义监控实例
第一章 自定义监控tcp状态 命令可以选择ss 或者 netstat ss打印基于socket的统计信息,实际运行下来,ss的速度要比netstat要快得多 1.1 tcp的十一种状态 ...
- Linux资源分析工具杂谈(长文慎入)
Linux资源分析工具杂谈 开篇之前请大家先思考一个问题: 磁盘的平均I/O响应时间是1 ms,这个指标是好,还是差? 众所周知,计算机科学是客观的,也就是说对于一个给定的问题,我们总是 ...
- 使用 dom4j 处理 xml (2)
记录一些 xpath 的常规用法,备忘. //3.XPath 了解(用于在xml中选取元素,类似 jquery 选择器) //3.1 路径 Node node1 = root.selectSingle ...
- 数据分片一致性hash
一致性hash 一致性hash是将数据按照特征值映射到一个首尾相接的hash环上,同时也将节点(按照IP地址或者机器名hash)映射到这个环上.对于数据,从数据在环上的位置开始,顺时针找到的第一个节点 ...