Given an array where elements are sorted in ascending order, convert it to a height balanced BST.


题目标签:Tree

  这道题目给了我们一个有序数组,从小到大。让我们把这个数组转化为height balanced BST。

  首先来看一下什么是binary search tree:

    每一个点的left < 节点 < right, 换一句话说,每一个点的值要大于左边的,小于右边的。

  那么什么是height balanced tree:

    每一个节点:左边的height (level) 和 右边的height (level) 只能相差1;比如说,一个点,它的左边的阶层,有4层,它右边的阶层有1层,相差3了,就不是height balanced tree。

  那么我们来分析一下怎么做这题,根据BST的特性,想象一下把一个BST树上下压缩,成一条线,那么这条线上从左到右的顺序是从小到大的。最中间的点就是root,所有左边的就是root的左边的children, 右边的就是root右边的children。有没有一种很熟悉的感觉?就和binary search一样原理。如果是给你一个array [1,2,3,4,5,6,7] 让你search一个数字n, 每次利用中间点来找,接着比较大小来分左右。这道题目等于是给了我们这个array,每次让我们找到中间点作为一个node,接着分为左右children,把范围分一下,继续递归下去。

Java Solution:

Runtime beats 14.29%

完成日期:07/02/2017

关键词:Tree

关键点:根据BST的特性,利用binary search来改变成递归function,代入值是nums array 和left right 范围两端的值

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public TreeNode sortedArrayToBST(int[] nums)
{
int left = 0;
int right = nums.length-1; return buildTree(nums, left, right);
} public static TreeNode buildTree(int[] nums, int left, int right)
{
if(left > right)
return null; // parent
int mid = left + (right - left) / 2;
TreeNode node = new TreeNode(nums[mid]); // left child
int leftChild_left = left;
int leftChild_right = mid - 1;
node.left = buildTree(nums, leftChild_left, leftChild_right); // right child
int rightChild_left = mid + 1;
int rightChild_right = right;
node.right = buildTree(nums, rightChild_left, rightChild_right); return node;
}
}

参考资料:

http://www.cnblogs.com/springfor/p/3879823.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)的更多相关文章

  1. [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)

    108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...

  2. 37. leetcode 108. Convert Sorted Array to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...

  3. LeetCode 108. Convert Sorted Array to Binary Search Tree (将有序数组转换成BST)

    108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...

  4. leetcode 108. Convert Sorted Array to Binary Search Tree 、109. Convert Sorted List to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 这个题使用二分查找,主要要注意边界条件. 如果left > right,就返回NULL.每次更新的 ...

  5. [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  6. Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  7. Java for LeetCode 108 Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...

  8. leetcode 108 Convert Sorted Array to Binary Search Tree ----- java

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 给一 ...

  9. LeetCode 108: Convert Sorted Array to Binary Search Tree DFS求解

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...

随机推荐

  1. POJ-2299 Ultra-QuickSort (树状数组,离散化,C++)

    Problem Description In this problem, you have to analyze a particular sorting algorithm. The algorit ...

  2. 分页复用代码【Page类、JSP显示页面】

    前言 为了复用,记载一些以前写过的工具类.方法 page类 import java.util.List; /** * Created by ozc on 2017/3/1. */ public cla ...

  3. 【转】SWT/JFace的对话框

    一.MessageDialog       ,MessageDialog的用法很简单 MessageDialog.openInfomation(shell,title,message);       ...

  4. 跨Storyboard调用

    在开发中我们会有这种需求从一个故事板跳到另一个故事板 modal UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:@ ...

  5. SLB vs CLB

    什么是SLB? SLB, 服务器负载均衡(Server Load Balancing),可以看作HSRP(热备份路由器协议)的扩展,实现多个服务器之间的负载均衡. 虚拟服务器代表的是多个真实服务器的群 ...

  6. Spring Boot-------JPA——EntityManager构建通用DAO

    EntityManager EntityManager 是用来对实体Bean 进行操作的辅助类.他可以用来产生/删除持久化的实体Bean,通过主键查找实体bean,也可以通过EJB3 QL 语言查找满 ...

  7. Two Sum IV - Input is a BST

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  8. httpd日志和日志轮替工具

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  9. 一张图理清ASP.NET Core启动流程

    1. 引言 对于ASP.NET Core应用程序来说,我们要记住非常重要的一点是:其本质上是一个独立的控制台应用,它并不是必需在IIS内部托管且并不需要IIS来启动运行(而这正是ASP.NET Cor ...

  10. SpringMVC框架(四)文件的上传下载,上下文路径

    文件目录: SpringMVC配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...