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 ...
随机推荐
- C# 递归查找文件夹下所有文件和子文件夹的所有文件
方法实现 public class DirectoryAllFiles { static List<FileInformation> FileList = new List<File ...
- EMQ 学习---订阅$SYS主题,捕获客户端上下线消息
acl.config文件定义了可订阅$SYS主题的权限. {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]} ...
- rownum使用说明
参考:http://www.blogjava.net/conans/articles/219693.html 参考:http://www.blogjava.net/freeman1984/archiv ...
- cmd隐藏指定文件
隐藏文件: 或者带路径执行: 显示文件:
- github 搜索技巧
1.关键词 + 指定开发语言 bitcoin language:javascript 2.关键词 + stars 数量 + forks 数量 bitcoin stars:> forks:>
- Mac OS 下安装mysql环境
传送门:Mac下安装与配置MySQL mac 上怎么重置mysql的root的密码? 一.下载mysql 进入官方下载地址:https://www.mysql.com/downloads/ 1.找 ...
- unity, remove a scene from build settings
把scene添加到build settings的scenes in build列表里以后,如果想删除,没有菜单可用,但选中按delete即可. 参考:http://answers.unity3d.co ...
- 多线程-Condition
关键字synchronized与wait和notify/notifyAll方法相结合可以实现等待/通知模式,类ReentrantLock也可以实现同样的功能,但需要借助于Condition对象.Con ...
- 王立平-- Swift
Swift是供iOS和OS X应用编程的新编程语言,基于C和Objective-C.而却没有C的一些兼容约束.Swift採用了安全的编程模式和加入现代的功能来是的编程更加简单.灵活和有趣. 界面则基于 ...
- python学习之platform模块
该模块用来访问平台相关属性. 常见属性和方法 平台架构 platform.machine() 返回平台架构.若无法确定,则返回空字符串. >>> platform.machine() ...