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

涉及到二叉树的问题用递归的方法很容易理解。这个问题要求把一个升序排序的链表转换为一颗height balanced BST。转换的方式就是把链表的中间值作为树的根节点,中间值的左半部分转换为二叉树的左子树,中间值的右半部分转换为二叉树的右子树。

递归解决问题的步骤如下:

1. 如果链表的长度为0,返回null;

2. 如果链表长度为1,创建新的二叉树节点node,node.left=null,node.right=null,node.val=head.val。

3. 否则的话找到链表的中间节点,即第mid = length%2==0?length/2:length/2+1个节点为根节点node。此时链表被分成两个部分,前半部分的长度为mid-1,后半部分的长度为length-mid,递归求这两部分的结果,并把他们分别赋值给根节点的左子树和右子树。代码如下:

 public class Solution {
public TreeNode sortedListToBST(ListNode head) {
ListNode pointer = head; int length = 0;
while(pointer!=null){
pointer = pointer.next;
length++;
} return listToBST(head, length); } public TreeNode listToBST(ListNode head,int length){
if(length==0) return null;
if(length==1){
TreeNode node = new TreeNode(head.val);
node.left = null;
node.right = null;
return node;
}
if(length==2){
TreeNode node = new TreeNode(head.val);
node.left = null;
node.right = listToBST(head.next, 1);
return node;
}
int mid = length%2==0?length/2:length/2+1; ListNode pointer = head;
ListNode righthead = null;
int i = 1;
while(pointer.next!=null && i<mid-1){
pointer = pointer.next;
i++;
}
TreeNode node = new TreeNode(pointer.next.val);
righthead = pointer.next.next;
pointer.next = null;
node.left = listToBST(head, mid-1);
node.right = listToBST(righthead, length-mid); return node;
}
}

LeetCode OJ 109. Convert Sorted List to Binary Search Tree的更多相关文章

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

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

  2. 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

  4. 【LeetCode OJ】Convert Sorted List to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ We design a ...

  5. LeetCode OJ 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. 把一 ...

  6. 【Leetcode】109. Convert Sorted List to Binary Search Tree

    Question: Given a singly linked list where elements are sorted in ascending order, convert it to a h ...

  7. LeetCode OJ: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 ...

  8. LeetCode OJ:Convert Sorted Array to Binary Search Tree(将排序好的数组转换成二叉搜索树)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 讲一 ...

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

随机推荐

  1. U盘安装VMware ESXi 6.0

    准备工作 在vmware官网注册,并获取ESXi 6.0 ISO Image: 下载UNetbootin: 容量1GB或以上的U盘,将其格式化. U盘制作 打开UNetbootin,如下图设置,文件路 ...

  2. hibernate异常:org.hibernate.MappingException

    这个是映射文件配置错误 异常:org.hibernate.MappingException 提示:Could not determine type for: java.lang,String, at ...

  3. 闭包&装饰器详解

    闭包 先不着急看闭包的定义,让我们从一段示例代码开始.如果将上一个示例稍微修改下: >>> def outer(): ... x = 1 ... def inner(): ... p ...

  4. ubuntu 14.04 安装matlab2015b(破解版),具体软件请访问我的网盘~

    本文章转载自:http://www.cnblogs.com/ttzm/p/5475086.html 1.下载matlab的Unix版本:安装文件放在某目录下(如在Downloads下,则文件的完整路径 ...

  5. CodeForces 705B Spider Man

    水题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...

  6. Eight

    Eight 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043/http://acm.split.hdu.edu.cn/showproblem.ph ...

  7. 为UIView添加分类直接改或获取控件的frame值

    #import <UIKit/UIKit.h> @interface UIView (SJBExtends) @property (nonatomic,assign)CGFloat x; ...

  8. Stack Overflow for web end

    Jquery UI tooltip on disabled button In general, disabled elements do not trigger any DOM events. Th ...

  9. input、button、a标签 等定义的按钮尺寸的兼容性问题

    在项目中有遇到这类问题,搜索了一下解决方式,采用链接:https://segmentfault.com/q/1010000000190931 里各位楼主的答案,摘抄如下: 例子如下: HTML: &l ...

  10. Apple 移动设备绑定动态生成元素点击事件$(document).on('click',element,callback)失效解决方法

    今天在工作中刚接触到了微信社区相关的开发工作,测试的时候发现,动态生成元素的点击事件在andriod设备上可以触发,而在apple移动设备上却无法触发.好奇的我赶紧百度了下,很快就在stackover ...