Java for LeetCode 109 Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
解题思路:
同上题,JAVA实现如下:
public TreeNode sortedListToBST(ListNode head) {
ArrayList<Integer> list=new ArrayList<Integer>();
while(head!=null){
list.add(head.val);
head=head.next;
}
return sortedListToBST(list,0,list.size()-1);
}
static public TreeNode sortedListToBST(ArrayList<Integer> list, int begin, int end) {
if (begin>end)
return null;
TreeNode root = new TreeNode(list.get((begin+end) / 2));
root.left=sortedListToBST(list,begin,(begin+end) / 2-1);
root.right=sortedListToBST(list,(begin+end) / 2+1,end);
return root;
}
Java for LeetCode 109 Convert Sorted List to Binary Search Tree的更多相关文章
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- Leetcode#109 Convert Sorted List to Binary Search Tree
原题地址 跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了. 代码: TreeNode *buildBST( ...
- 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. 解题 ...
- leetcode 109 Convert Sorted List to Binary Search Tree ----- java
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [leetcode]109. Convert Sorted List to Binary Search Tree链表构建二叉搜索树
二叉树的各种遍历方式都是可以建立二叉树的,例如中序遍历,就是在第一步建立左子树,中间第二步建立新的节点,第三步构建右子树 此题利用二叉搜索树的中序遍历是递增序列的特点,而链表正好就是递增序列,从左子树 ...
- [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 ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 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.每次更新的 ...
- 108. Convert Sorted Array to Binary Search Tree 109. Convert Sorted List to Binary Search Tree -- 将有序数组或有序链表转成平衡二叉排序树
108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...
随机推荐
- php根据日期时间生成随机编码
订单.申请单之类的需要一个编码,通过下面方法可得 代码如下: $PNUM="PG" . date("YmdHis").rand(1000,9999);
- Redis 在 Java 中的使用
转:http://blog.csdn.net/jiangtao_st/article/details/8256610 一.下载jar包 https://github.com/xetorthio/jed ...
- scramble-string——两个字符串经过树化并旋转后是否一致、递归、动态规划
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 利用window.navigator.userAgent判断当前是否微信内置浏览器
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8&quo ...
- android 怎样单独下载一个项目
起因,"网络"不太好."比方铁通的就是不如联通的" 每次运行一边repo sync,十分蛋疼,假设不做full build无需所有下载,着急看某个项目的修改但是 ...
- phpstorm 设置
phpstorm 设置多项目并存 phpstorm 或 webstorm 设置多个项目可以并存: File -> settings -> Directories -> Add Co ...
- 非spring托管的类使用spring脱管的类。
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte ...
- com.opensymphony.xwork2.ognl.OgnlValueStack - Error setting expression 'customer.applyRate' with value '[Ljava.lang.String;@7d3fae2c'
出错的3个最可能的原因:我是第二种错误 1.action配置错误 <action name="doCreateMeetingInfo" class="meeting ...
- spark on yarn 配置history server
spark在yarn模式下配置history server 1.建立hdfs文件– hadoop fs -mkdir /user/spark– hadoop fs -mkdir /user/spark ...
- linux支持的machine-types
在内核文件中arch/arm/tools/mach-types定义目前内核支持的板卡.芯片等: ##machine_is_xxx CONFIG_xxxx MACH_TYPE_xxx number ...