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. 网络最大流最短增广路Dinic算法模板

    #include<cstdio> #include<cstring> #include<string> #include<cmath> #include ...

  2. LOOPS

    LOOPS 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3853 递推 设dp[i][j]为(i,j)到终点期望的使用魔力值,mp[i][ ...

  3. 【Python@Thread】thread模块

    一.关于Python多线程 Python解释器中可以同时运行多个线程,但是再任意时刻只能有一个线程在解释器运行. Python虚拟机的访问是由全局解锁器(GIL)控制的,由GIL保证同时只有一个线程的 ...

  4. 【Python】关于Python有意思的用法

    开一篇文章,记录关于Python有意思的用法,不断更新 1.Python树的遍历 def sum(t): tmp=0 for k in t: if not isinstance(k,list): tm ...

  5. js for...in 语句

    原文链接:http://www.w3school.com.cn/js/js_loop_for_in.asp for...in 语句用于遍历数组或者对象的属性(对数组或者对象的属性进行循环操作). 实例 ...

  6. Java JVM 请别拿“String s=new String("z");创建了多少实例”来面试 [ 转载 ]

    Java 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧 [ 转载 ] @author RednaxelaFX 原文链 ...

  7. List container

    //List容器 //List本质是一个双向链表 //构造函数 list<int>c0; //空链表 list<int>c1(3); //建一个含三个默认值是0的元素链表 li ...

  8. Java常用集合类(1)

    一.HashMap 参考文章: http://yikun.github.io/2015/04/01/Java-HashMap%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E ...

  9. LeetCode OJ 1. Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  10. Android JNI的使用浅析

    介绍JNI的好文章: http://blog.csdn.net/yuanzeyao/article/details/42418977 JNI技术对于多java开发的朋友相信并不陌生,即(java na ...