public TreeNode sortedListToBST(ListNode head) {
if(head==null) return new TreeNode(0);
ArrayList<TreeNode> arr=new ArrayList<TreeNode>();
while(head!=null)
{
arr.add(new TreeNode(head.val));
head=head.next;
} return BST(arr,0,arr.size()-1);
}
TreeNode BST(ArrayList<TreeNode> list,int start,int end)
{
if(start>=end)
{
return list.get(start);
}
else
{
int mid=start+(end-start)/2;
TreeNode root=list.get(mid);
root.left=BST(list,start,mid-1);
root.right=BST(list,mid+1,end); return root;
}
}

Convert Sorted List to Binary Search Tree java的更多相关文章

  1. 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 ...

  2. 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. 给一 ...

  3. [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 ...

  4. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  5. leetcode -- 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 ...

  6. LeetCode: Convert Sorted List to Binary Search Tree 解题报告

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

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 初学jquery遇见的两个小问题!

    <body>    <div id="divtest">div的内容</div>    <div id="default&quo ...

  2. Button简单实例1

    1.XML按钮定义 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" an ...

  3. Android-------手机屏幕适配之文件适配

    public class Main {         //定义文件本地存储路径,可按照需求更改         private final static String rootPath = &quo ...

  4. c3p0连接数据库

    <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-con ...

  5. iOS方法类:CGAffineTransform的使用

    CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放.旋转和平移操作: 另称放射变换矩阵,可参照线性代数的矩阵实现方式0. ...

  6. 链表中倒数第K个节点

    问题描述: 找出链表中倒数第K个节点 思路分析: 用两个指针,一前一后,保持k个距离,前面的指针移动到末尾,后面的指针就刚好直到第k个节点, 要考虑到k为0,倒数第k个节点不存在的情况. 参考代码: ...

  7. Reward HDU

    Reward                                    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32 ...

  8. BeanUtils包的学习

    BeanUtils支持八种基本数据类型(int double short char byte float boolean long)的反射,对于日期需要提前注册DateLocalConvert获取转换 ...

  9. linux 安装mysqlServer

    先下载mysql安装包 打开 http://dev.mysql.com/downloads/mysql/  选择 linux - Generic 再选择*.tar.gz(最后那两个) 下载完毕后,得到 ...

  10. wordpress教程之自带缩略图功能

    首页你要看下你所用的主题有没有开启文章缩略图功能,如果看起的话,会在wordpress后台编辑页面或者文章时在右下角的地方看到一个特色图像的设置,如下图: 如果没有说明你还没有激活这功能.我们需要在你 ...