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. 远程连接Windows2008R2时服务器报Terminal Services错误的解决办法

    症状: 使用终端服务客户端连接到Windows Server2008 R2的机器时,如果客户端选项中选择了打印机(注1)时,它可能会在在系统事件日志中记录一个TerminalServices打印机错误 ...

  2. 去掉NavigationBar底部的黑线

    UINavigationBar *navigationBar = self.navigationController.navigationBar;   [navigationBar setBackgr ...

  3. vc设置窗口透明

    ::SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, ::GetWindowLongPtr(GetSafeHwnd(), GWL_EXSTYLE) | WS_EX_L ...

  4. (spring-第13回【IoC基础篇】)PropertyEditor(属性编辑器)--实例化Bean的第五大利器

    上一篇讲到JavaBeans的属性编辑器,编写自己的属性编辑器,需要继承PropertyEditorSupport,编写自己的BeanInfo,需要继承SimpleBeanInfo,然后在BeanIn ...

  5. Osmocom-BB 相关资源、知识分享

    1.在layer1层添加了解析sniffer的代码 参考http://git.osmocom.org/osmocom-bb/log/?h=luca/gsmmap)osmocom-bb/src/targ ...

  6. 【LeetCode OJ】Linked List Cycle

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle/ We set two pointers: the faster poi ...

  7. VBS_For Each...Next

    For Each...Next 循环与 For...Next 循环类似.For Each...Next 不是将语句运行指定的次数,而是对于数组中的每个元素或对象集合中的每一项重复一组语句.这在不知道集 ...

  8. PHP 防范IP攻击

    <?php //查询禁止IP $ip =$_SERVER['REMOTE_ADDR']; $fileht=".htaccess2"; if(!file_exists($fil ...

  9. Inno Setup 安装、卸载前检测进程或服务

    [转载]Inno Setup 安装.卸载前检测进程或服务 (2015-04-24 17:37:20) 转载▼ 标签: 转载   原文地址:Inno Setup 安装.卸载前检测进程或服务作者:一去丶二 ...

  10. magento数据库备份导入还原

    Magento数据库备份.移植终极解决方案+3 分类:Magento教程 标签:magento搬家.magento数据库备份.magento更换域名.magento移植 4,355人浏览 作为电子商务 ...