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

Solution:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedListToBST(ListNode head) {
if (head==null)
return null; ListNode end = head;
while (end.next!=null){
end = end.next;
}
TreeNode root = sortedListToBSTRecur(head,end); return root;
} //Convert the list from head to end into BST, return the root node
public TreeNode sortedListToBSTRecur(ListNode head, ListNode end){
//If only one node.
if (head==end){
TreeNode root = new TreeNode(head.val);
return root;
} //If only two node.
if (head.next==end){
TreeNode root = new TreeNode(head.val);
TreeNode child = new TreeNode(end.val);
root.right = child;
return root;
} //Otherwise, count the number of node in the list, find out the median one,
//and convert the left list and right list to BST.
int nodeNum = 1;
ListNode curNode = head;
while (curNode!=end){
curNode = curNode.next;
nodeNum++;
}
int mid = nodeNum/2+nodeNum%2;
ListNode median = null;
ListNode pre = null;
curNode = head;
//NOTE: we only need to move (mid-1) steps to move curNode to the median one.
for (int i=0;i<mid-1;i++){
pre = curNode;
curNode = curNode.next;
}
median = curNode;
TreeNode root = new TreeNode(median.val);
TreeNode leftChild = sortedListToBSTRecur(head,pre);
TreeNode rightChild = sortedListToBSTRecur(median.next,end);
root.left = leftChild;
root.right = rightChild;
return root;
}
}

For a list from head to end, we find out the median node and use recursion method to convert its left list and right list to BST, and then connect the left subtree and right subtree to the median node.

Leetcode-Convert Sorted List to BST.的更多相关文章

  1. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  2. 109.Convert sorted list to BST

    /* * 109.Convert sorted list to BST * 2016.12.24 by Mingyang * 这里的问题是对于一个链表我们是不能常量时间访问它的中间元素的. * 这时候 ...

  3. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  4. [LeetCode] 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 ...

  5. [LeetCode] 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 -- 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 ...

  7. LeetCode: Convert Sorted List to Binary Search Tree 解题报告

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  8. LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  9. LeetCode——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 ...

  10. LeetCode - Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

随机推荐

  1. golang中使用mongodb

    mgo简介 mongodb官方没有关于go的mongodb的驱动,因此只能使用第三方驱动,mgo就是使用最多的一种. mgo(音mango)是MongoDB的Go语言驱动,它用基于Go语法的简单API ...

  2. EMQ 学习---订阅$SYS主题,捕获客户端上下线消息

    acl.config文件定义了可订阅$SYS主题的权限. {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]} ...

  3. python selenium --unittest 框架

    转自:http://www.cnblogs.com/fnng/p/3300788.html 学习unittest 很好的一个切入点就是从selenium IDE 录制导出脚本.相信不少新手学习sele ...

  4. 如果你需要从不同的服务器(不同域名)上获取数据就需要使用跨域 HTTP 请求

    Response.AppendHeader("Access-Control-Allow-Origin", "*")Response.AppendHeader(& ...

  5. Redis(三):windows下Redis的安装配置以及注意事项

    一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载 官网下载地址 ...

  6. 强大的响应式jQuery消息通知框和信息提示框插件

    lobibox是一款功能很强大的jQuery消息通知框和信息提示框插件.这个插件分为两个部分:消息通知框和信息提示框.它能很好的结合Bootstrap使用. 信息提示框 lobibox的信息提示框能够 ...

  7. atitit.it企业管理 项目管理 中的 授权机制 的来源 君权神授 的一定合理性

    atitit.it企业管理 项目管理 中的 授权机制 的来源 君权神授 的一定合理性 1. 授权(权利来源)的5种模式 1 2. 企业的组织机构与管理运作来源于国家的管理...而国家的管理又来源于宗教 ...

  8. 哪个线程执行 CompletableFuture’s tasks 和 callbacks?

    CompletableFuture尽管在2014年的三月随着Java8被提出来,但它现在仍然是一种相对较新潮的概念.但也许这个类不为人所熟知是好事,因为它很容易被滥用,特别是涉及到使用线程和线程池的时 ...

  9. HDU 1358 Period(kmp简单解决)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...