Leetcode-Convert Sorted Array to BST
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Solution:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] num) {
if (num.length==0)
return null; int end = num.length-1;
TreeNode root = sortedArrayToBSTRecur(num,0,end);
return root;
} public TreeNode sortedArrayToBSTRecur(int[] num, int head, int end){
if (head==end){
TreeNode root = new TreeNode(num[head]);
return root;
} if (head+1==end){
TreeNode root = new TreeNode(num[end]);
TreeNode child = new TreeNode(num[head]);
root.left = child;
return root;
} //Calculate the median index.
int len = end-head;
int mid = head + len/2 + len%2;
TreeNode root = new TreeNode(num[mid]);
TreeNode leftChild = sortedArrayToBSTRecur(num,head,mid-1);
TreeNode rightChild = sortedArrayToBSTRecur(num,mid+1,end);
root.left = leftChild;
root.right = rightChild;
return root;
}
}
Leetcode-Convert Sorted Array to BST的更多相关文章
- 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 ...
- 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] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- LeetCode - Convert Sorted Array to Binary Search Tree
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]
1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...
- LeetCode——Convert Sorted Array to Binary Search Tree
Description: Given an array where elements are sorted in ascending order, convert it to a height bal ...
- LeetCode Convert Sorted Array to Binary Search Tree(数据结构)
题意: 将一个有序的数组建成一棵平衡的BST树. 思路: 因为数组已经有序,每次可以从中点开始建根,再递归下去分别处理左/右子树. /** * Definition for a binary tree ...
- [leetcode]Convert Sorted Array to Binary Search Tree @ Python
原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉 ...
- [leetcode]Convert Sorted Array to Binary Search Tre
排好序的... 中间是root , root左边是left,root右边是right 递归建树. /** * Definition for binary tree * struct TreeNode ...
- 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 ...
随机推荐
- 全面进攻python之前回顾下自己近三个月的自学之路
人生是在一直试错的过程中成长起来的.这句话貌似很有道理,但回顾了下自己近三个月python自学学习之路,又觉得自己对这句话又有了新的看法------行动之前必须要有正确的选择,这样做错了才能成长. 2 ...
- SqlCommandBuilder的作用
最近看一个案例,关于如何向数据库传数据,颇有发现. 以前我的数据都是采用Sql代码用SqlCommand一条一条语句的传入数据库. 但是今天发现有更好的方法. 代码如下: SqlConnection ...
- 删除MYSQL账号多于的空用户
默认情况下,mysql安装好之后,会存在匿名用户,也可以叫空用户,输入mysql之后直接回车便可进入mysql. 该匿名用户具有一定的权限,通过SHOW DATABASES;可以查看到informat ...
- php 验证访问浏览器是电脑还是手机
//判断如果为手机用户则 if(checkmobile()==true) { $url=$_SERVER["HTTP_HOST"]; $urlHost=GetHost($url); ...
- unity5, animator state machine, 无条件transition实现播放动画序列
今天遇到这样一个需求,我有一个名为happy的animation clip和一个名为speak的animation clip.想实现当主角胜利后播放动序列: happy->speak->h ...
- wpf 帧动画
帧动画实现很简单 <ImageBrush x:Key="speed_s" Stretch="Fill" ImageSource="/images ...
- Atitit.二维码功能的设计实践 attilax 总结
Atitit.二维码功能的设计实践 attilax 总结 1.1. 二维码要实现的功能1 1.2. 现有二维码功能设计不足的地方(待改进)1 1.3. 二维码组件1 1.4. Java版 zxing ...
- SVN使用(自己总结)
1.第一次导入,注意要新建个readme.txt文件用于记录版本更改,每次导入时 要填写import message 2.第二次增加文件导入时 勾选未受控版本文件(新增加文件) 完成上传就可更新增加的 ...
- [elk]logstash grok原理
logstash语法 http://www.ttlsa.com/elk/elk-logstash-configuration-syntax/ https://www.elastic.co/guide/ ...
- 神器 cmder
神器 cmder .wmd-input, .wmd-input:focus, #md-section-helper {font-size: 14px !important;line-height: 2 ...