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.



题目标签: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 范围两端的值

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution
11 {
12     public TreeNode sortedArrayToBST(int[] nums)
13     {
14         int left = 0;
15         int right = nums.length-1;
16
17         return buildTree(nums, left, right);
18     }
19
20     public static TreeNode buildTree(int[] nums, int left, int right)
21     {
22         if(left > right)
23             return null;
24
25
26         // parent
27         int mid = left + (right - left) / 2;
28         TreeNode node = new TreeNode(nums[mid]);
29
30         // left child
31         int leftChild_left = left;
32         int leftChild_right = mid - 1;
33         node.left = buildTree(nums, leftChild_left, leftChild_right);
34
35         // right child
36         int rightChild_left = mid + 1;
37         int rightChild_right = right;
38         node.right = buildTree(nums, rightChild_left, rightChild_right);
39
40         return node;
41     }
42 }

参考资料:

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

时间: 2024-12-15 01:56:59

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

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

leetCode 108.Convert Sorted Array to Binary Search Tree(将排序数组转换为BST) 解题思路和方法

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路:将排序数组转换为高度平衡的二叉搜索树.思想是将中间的值作为根节点,然后左右的数组分别为左右子树.递归求解. 代码如下: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode

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. 解题思路: 首先要理解,什么叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然后就十分容易了,JAVA实现如下: public TreeNode sortedArrayToBST(int[] nums) { return

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. 给一个排好序的数组,然后求搜索二叉树 其实就是二分法,不难. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNo

[leetcode]108. Convert Sorted Array to Binary Search Tree构建二叉搜索树

构建二叉搜索树 /* 利用二叉搜索树的特点:根节点是中间的数 每次找到中间数,左右子树递归子数组 */ public TreeNode sortedArrayToBST(int[] nums) { return builder(nums,0,nums.length-1); } public TreeNode builder(int[] nums,int left,int right) { if (left>right) return null; int mid = (left+right)/2;

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

题目链接:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 108. Convert Sorted Array to Binary Search Tree My Submissions Question Total Accepted: 68378 Total Submissions: 187560 Difficulty: Medium Given an array where elements ar

[Leetcode][BST][Convert Sorted Array to Binary Search Tree]

把一个排好序的vector转换为一颗二分查找树. 很简单的题目,递归即可,保证边界不要出错. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 1

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,

【一天一道LeetCode】#108. Convert Sorted Array to Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. (二)解题 题目大意:给定一个排好序的数组,将它转变成一个平衡二叉搜索数 解题思路:平衡二叉搜索树的中序遍历为升序数组