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. jQuery 中的 Ajax

    jQuery 对 Ajax 操作进行了封装, 在 jQuery 中最底层的方法时 $.ajax(), 第二层是 load(), $.get() 和 $.post(), 第三层是 $.getScript ...

  2. 【转】linux /centos 中OpenSSL升级方法详解

    相关软件下载地址 Apache:http://httpd.apache.org/ Nginx:http://nginx.org/en/download.html OpenSSL:http://www. ...

  3. idea修改jsp后不会自动编译和替换(转)

    使用IntelliJ IDEA开发JavaWeb项目时,修改了JSP后刷新浏览器无法及时显示修改后的页面? 解决办法:tomcat配置中,On frame deactivation属性选择Update ...

  4. Iterator和ListIterator的区别

    Iterator是ListIterator的父接口. Iterator是Collection中取元素的方式,ListIterator局限于List Iterator只有hasNext(),next() ...

  5. android 单选、多选弹出菜单

    菜单单选窗口: import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInte ...

  6. jsCodeWar 多函数嵌套调用

    function compose(f, g) { return function() { return f(g.apply(this, arguments)); }; } --- function c ...

  7. 极客DIY:廉价电视棒玩转GNSS-SDR,实现GPS实时定位

    0×00 前言 GNSS是Global Navigation Satellite System的缩写.中文称作:全球卫星导航系统.全球导航卫星系统. GNSS泛指所有的卫星导航系统,包括全球的.区域的 ...

  8. python3爬虫再探之EXCEL

    在爬取数据之后,数据的保存就成为一个新的问题,一般不太大的的数据存储到EXCEL就可以了.这里介绍一个python的第三方库——xlsxwriter. 这个库的安装就不介绍了,pip就可以,不用FQ. ...

  9. iOS提交AppStore被拒原因

    1. Terms and conditions(法律与条款) 1.1 As a developer of applications for the App Store you are bound by ...

  10. CentOS 7.0禁用iptables防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. firewall: systemctl start firewalld.service#启动firewal ...