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. mysql中如何统计某字段里某个字符的个数

    select * from order where length(order_num)-length(replace(order_num,'8','')) = 4

  2. java基础讲解03-----java的结构

    前面我们说了java是面向对象的语言,java程序的基本组成单元是类,类中又属性,方法两个部分,每个应用程序都会有一个mian函数,含有main()方法的类,我们称为主类 package  Test; ...

  3. python selenium --命令之文字范本匹配

    文字范本匹配 ======================================= 文字范本匹配其实可以理解为通配符.我想大家都用过windows 系统自带的搜索功能. *   星号代表一个 ...

  4. 关于JSP和HTML

    工作中,发现非常多同事不清楚JSP与HTML的生命周期.以至于出现"JavaScript为啥不能调用JSTL标签(或EL表达式)?"的笑话问题. 以下以流程图的方式.简单说明一下过 ...

  5. sklearn 随机森林方法

    Notes The default values for the parameters controlling the size of the trees (e.g. max_depth, min_s ...

  6. Myeclipse中 Exploded location overlaps an existing deployment解决办法

    实效解决方法: 项目->properties->MyEclipse->Web->Web Context-root的名字为重命名之后的名字即可 其实这里的Web Context- ...

  7. href中使用相对路径访问上级目录的方法

    项目ProjectXXX目录如下: WebContent> hello.jsp Folder1> foo.jsp Folder2> foo2.jsp 在foo.jsp中访问hello ...

  8. 基于Java Mina框架的部标jt808服务器设计和开发

    在开发部标GPS平台中,部标jt808GPS服务器是系统的核心关键,决定了部标平台的稳定性和行那个.Linux服务器是首选,为了跨平台,开发语言选择Java自不待言.需要购买jt808GPS服务器源码 ...

  9. impala+hdfs+csv格式文件

    [创建目录]hdfs dfs -mkdir -p /user/hdfs/sample_data/csv/devicehdfs dfs -mkdir -p /user/hdfs/sample_data/ ...

  10. 反向传播BackPropagation

    http://www.cnblogs.com/charlotte77/p/5629865.html http://www.cnblogs.com/daniel-D/archive/2013/06/03 ...