Leetcode-Convert Sorted List to BST.
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.的更多相关文章
- 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 ...
- 109.Convert sorted list to BST
/* * 109.Convert sorted list to BST * 2016.12.24 by Mingyang * 这里的问题是对于一个链表我们是不能常量时间访问它的中间元素的. * 这时候 ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 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 ...
- [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. 这道 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Azure Storage Blob 属性设置
概述 在使用SDK做Blob对象属性的获取或设置时,如果只是直接使用get或set方法,是无法成功获取或设置blob对象的属性.主要是因为在获取对象时,对象的属性默认并未被填充到对象,这就需要执行额外 ...
- python ---用户输入
范例1:我们希望整数(整数),这就是为什么我们使用int()函数. x = int(raw_input("Enter x:")) y = int(raw_input("E ...
- Service stopSelf(int statId)和onStartcommand(Intent intent,int flags,int startId)
Stopping a service A started service must manage its own lifecycle. That is, the system does not sto ...
- instanceof 与typeof的用法
通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型.例如: var oStringObject = new String("hello world"); con ...
- jquery 在ul中取得第一级的li
在ul中取得第一级的li <div id='demo1'> <ul> <li id='1'>1<li> <li id='2'>2<li ...
- MSBuild入门(续)
MSBuild基本概念(续) 在上一篇简单的介绍了下MSBuild中的四个基本块,每块介绍比较单薄,在这里对在大多数的项目模版生成的*.*proj文件中比较常见一些用法和概念做些补充.主要有一下几方面 ...
- Docker Python API 与 Docker Command
span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...
- ArcGIS教程:Geostatistical Analyst 应用演示样例
Geostatistical Analyst 满足多种不同应用的需求. 下面是 Geostatistical Analyst 的一小部分应用演示样例. 探索性空间数据分析 Geostatistical ...
- HTML5 多图上传
HTML5 多图上传 时间 2014-06-05 16:06:29 月小升博客 原文 http://java-er.com/blog/html5-many-image-upload/ 主题 HTM ...
- Jquery js框架使用
jquery 众所周知 ,强大的 js框架 自己使用的一些笔记 //1.json格式定义方法 var product_obj={ check_init:function(){ ...