Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

 
和上一题类似,把数组换成链表,所以可以两种做法:
1、把链表换成数组,然后用上一题的方法,这样会比较慢。
2、每次找到中间的点,作为节点,然后递归,其实原理还是二分查找。
 
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* 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 sortedListToBST(ListNode head) {
List list = new ArrayList<Integer>();
if( head == null)
return null;
return helper(head,null);
}
public TreeNode helper(ListNode head,ListNode target){ if( head == target )
return null;
ListNode node1 = head;
ListNode node2 = head; while( node2 != target && node2.next != target){
node1 = node1.next;
node2 = node2.next.next;
}
TreeNode node = new TreeNode(node1.val); node.left = helper(head,node1);
node.right = helper(node1.next,target); return node; }
}

第一种:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* 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 sortedListToBST(ListNode head) {
List list = new ArrayList<Integer>();
if( head == null)
return null;
while( head != null ){
list.add(head.val);
head = head.next;
}
int[] nums = new int[list.size()];
for( int i = 0;i<list.size();i++)
nums[i] = (int) list.get(i);
return sortedArrayToBST(nums);
}
public TreeNode sortedArrayToBST(int[] nums) {
int len = nums.length;
return helper(nums,0,(len-1)/2,len-1); } public TreeNode helper(int[] nums,int start,int mid,int end){ if( start > end )
return null;
TreeNode node = new TreeNode(nums[mid]); node.left = helper(nums,start,(mid+start-1)/2,mid-1); node.right = helper(nums,mid+1,(end+mid+1)/2,end); return node; }
}

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

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

  2. Leetcode#109 Convert Sorted List to Binary Search Tree

    原题地址 跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了. 代码: TreeNode *buildBST( ...

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

  4. [leetcode]109. Convert Sorted List to Binary Search Tree链表构建二叉搜索树

    二叉树的各种遍历方式都是可以建立二叉树的,例如中序遍历,就是在第一步建立左子树,中间第二步建立新的节点,第三步构建右子树 此题利用二叉搜索树的中序遍历是递增序列的特点,而链表正好就是递增序列,从左子树 ...

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

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

  7. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  8. 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.每次更新的 ...

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

随机推荐

  1. Linux gcc编译(动态库,静态库)

    1. linux 库路径: /lib , /usr/lib , /usr/local/lib 2.linux 编译静态库 a.编写源文件vi pr1.c void print1(){    print ...

  2. 从协议VersionedProtocol开始1

    Phase 0: Make a plan You must first decide what steps you're going to have in your process. It sound ...

  3. powershell 判断操作系统版本 命令

    powershell 传教士 原创文章.始于 2015-12-15 允许转载,但必须保留名字和出处,否则追究法律责任 一 前言 判断操作系统版本,是个老话题,bat.vbs中都有例子,这本不是重要问题 ...

  4. Hadoop c++开发

    假设你有上百G的数据,你要统计出这些数据中,含有某些你感兴趣的内容的数据的有多少条,你会怎么做?在硬件条件允许的情况下,用hadoop并行计算是一个不错的选择. 为了使本文得以清晰地说明,我们不妨假设 ...

  5. BZOJ 1486 最小圈

    二分答案是显然的,我们需要dfs版spfa判一下负环. 看起来是n^2其实很快. #include<iostream> #include<cstdio> #include< ...

  6. HTTP请求错误大全

    HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败 HTTP 401.2 - 未授权:服务器配置问题导致登录失败 HTTP 401.3 - ACL 禁止访问资源 HTTP 401 ...

  7. magneto创建运费模板

    Magento系统自带了大概7种运费方式:平价.运费表.免运费.ups.usps.fedex.dhl等.不过这些依然无法满足我们的需求,这时候就需要创建一个shipping module 来实现了.创 ...

  8. magento -- 添加中国省份列表

    magento本身的数据库里不包含中国的省份信息,你可以执行以下的SQL语句来添加 添加到directory_country_region表 INSERT INTO  `directory_count ...

  9. 第三个Sprint完结工作 用场景来规划测试工作.

    一.根据用户使用场景测试: 1.流程 典型群体 群体 张小明 年龄 7-12岁 职业 小学生 收入 压岁钱还有零花钱 能力 看一些简单的数,做一些相对简单的事 爱好 玩游戏 典型场景 张小明平时喜欢玩 ...

  10. 转:Repeater嵌套绑定Repeater以及内层调用外层数据

    <table border=" style="margin-bottom: 5px" width="100%"> <asp:Repe ...