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. python如何保证多个线程同时修改共享对象时不出错!

    import threadingimport timenumber = 0lock = threading.RLock() #是Lock()的升级版,用Rlock()即可def run(num): l ...

  2. power oj 1557种树[二进制状压DP]

    题目链接[https://www.oj.swust.edu.cn/problem/show/1557] 题意:中文题目. 题解:用0,1表示某个位置是否种了树,先算出同一行的有效状态的总数,即开两个1 ...

  3. Git忽略对特定文件的跟踪和提交

    1.有未提交过的文件,并且此文件项目组中的其他人员也需要忽略,可将此文件的完整路径写入项目文件夹下的.gitignore文件. 2.有未提交过的文件,此这些文件与项目组中的其他人员无关,毋须写入.gi ...

  4. android源码查看所有分支切换分支

    cd .repo/manifests git branch -a repo init -b android-4.1.2_r1 repo sync

  5. HDU 5784 How Many Triangles

    计算几何,极角排序,双指针,二分. 直接找锐角三角形的个数不好找,可以通过反面来求解. 首先,$n$个点最多能组成三角形个数有$C_n^3$个,但是这之中还包括了直角三角形,钝角三角形,平角三角形,我 ...

  6. 第九十三节,html5+css3移动手机端流体布局,基础CSS,头部设计,轮播设计,底部设计

    html5+css3移动手机端流体布局,基础CSS,头部设计,轮播设计,底部设计 基础CSS 首先将通用css属性写好 @charset "utf-8"; /*通用样式*/ /*去 ...

  7. php获取本周周一、周日时间,上周周一、周日时间,本月第一天,本月最后一天,上个月第一天,最后一天时间

    权声明:本文为博主原创文章,未经博主允许不得转载. //这个星期的星期一 // @$timestamp ,某个星期的某一个时间戳,默认为当前时间 // @is_return_timestamp ,是否 ...

  8. Storm源码分析--Nimbus-data

    nimbus-datastorm-core/backtype/storm/nimbus.clj (defn nimbus-data [conf inimbus] (let [forced-schedu ...

  9. 好友与组--ESFramework 4.0 进阶(11)

    大部分分布式通信系统中,都会涉及到客户端之间相互通信.以及需要将客户端进行分组的功能,或者是类似这方面的需求.ESFramework对这一常见的任务内置了强大的支持,包括从客户端到服务端.一直到Pla ...

  10. UVALive 6948 Jokewithpermutation 深搜

    题意就是把一段序列拆成从1到n的形式 一开始暴力了一下 后来发现bug太多一定是思路不对…… #include<stdio.h> #include<iostream> #inc ...