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. Petya and Spiders【二进制状压】

    题目链接[http://codeforces.com/problemset/problem/111/C] 题意:给出大小为N*M的图(1 ≤ n, m ≤ 40, n·m ≤ 40),每个图中有一个蜘 ...

  2. FireFox的配置文件的引用

    1.firefox的配置文件的实际路径:C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\oviavula.default ...

  3. 今天写动态canvas柱状图小结

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. Android原生Cling演化的覆盖层

    package com.example.demotest; import android.content.Context; import android.graphics.Paint; import ...

  5. Linux关闭selinux

    最近在折腾Linux服务器,配置环境时,安装php的一个拓展,明明编译成功了,就是加载不进去,phpinfo不显示,查看错误日志是显示加载失败,没权限==,配置ftp程序也会有意想不到的问题,搞了好久 ...

  6. zoj 3204 Connect them

    最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...

  7. win10应用UserControl

    <Grid xmlns:src="using:UserControlExample" Margin="0,50,0,0"> <Grid.Row ...

  8. java中的日期处理

    学习Java日期处理,看见这一篇比较详细,转载之. 转自:http://www.cnblogs.com/hqr9313/archive/2012/04/19/2458221.html   时间日期1) ...

  9. Java 集合 持有引用 & WeakHashMap

    Java 集合 持有引用 & WeakHashMap @author ixenos 摘要:强引用.弱引用.WeakHashMap动态回收步骤 Reference引用对象 可获得的(reacha ...

  10. csu oj Infected Computer 1427

    #include <iostream> #include <algorithm> #include <stdio.h> #define max 20005 #def ...