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. jQuery Growl 插件(消息提醒)

    jQuery Growl 插件(消息提醒) 允许您很容易地在一个覆盖层显示反馈消息.消息会在一段时间后自动消失,不需要单击"确定"按钮等.用户也可以通过移动鼠标或点击关闭按钮加快隐 ...

  2. python selenium --处理下拉框

    下拉框是我们最常见的一种页面元素,对于一般的元素,我们只需要一次就定位,但下拉框里的内容需要进行两次定位,先定位到下拉框,再定位到下拉框内里的选项. drop_down.html <html&g ...

  3. 关于Go语言daemon启动的方法.

    昨天搞了个文件共享的小程序,遇见了意见蛋疼的事,就是启动之后终端不能关闭,不然程序也会随着关闭. 我的解决方法: nohup ./httpserver & nohup这个命令能够把程序放后台执 ...

  4. ssh理论知识

    一.spring工作原理: 1.spring mvc请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作. 2.DispatcherSer ...

  5. unity 已知cosA和sinA,求A

    和c++中的atan2(y,x)类似,unity中有也Mathf.Atan2(y,x).

  6. 多线程-ThreadLocal,InheritableThreadLocal

    ThreadLocal 变量值得共享可以使用public static变量的形式,所有的线程都使用同一个public static变量.如果想实现每一个线程都有自己的共享变量该如何解决呢?JDK中提供 ...

  7. C++使用ADO存取图片

     在项目中.我们须要把事故简图上传到总server.以便每一个client都能下载或者查看.在网上找了找,向Server2000存储图片代码比較多,从数据库中读取图片并显示也不少,可是把图片从数据 ...

  8. 李洪强和你一起学习前端之(7)定位盒子 css可见性 滑动门案例

    今天是2017年3月23日 1 复习昨天知识 1.1浮动 Float:left | right 特点: ->浮动的元素不占位置(脱标) ->可以将行内元素转化为行内块元素 ->块级元 ...

  9. Oracle配置客户端

    一.引言 当我们需要连接远程的Oracle数据库服务器时,就需要在自己的机器上安装Oracle客户端了. 二.安装步骤与配置 参考:http://blog.csdn.net/luiseradl/art ...

  10. VirtualBox虚拟机和主机之间的通信

    - VirtualBox的NAT网络模式,主机不能访问虚拟机- 端口转发可以访问,但是性能非常差,第一次连接30秒左右- 有条件的还是推荐VmWare