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 ascending order, convert it to a height balanced BST.
SOLUTION 1:
public TreeNode sortedListToBST1(ListNode head) {
ListNode fast = head;
ListNode slow = head; ListNode pre = head; if (head == null) {
return null;
} TreeNode root = null;
if (head.next == null) {
root = new TreeNode(head.val);
root.left = null;
root.right = null;
return root;
} // get the middle node.
while (fast != null && fast.next != null) {
fast = fast.next.next; // record the node before the SLOW.
pre = slow;
slow = slow.next;
} // cut the list to two parts.
pre.next = null;
TreeNode left = sortedListToBST1(head);
TreeNode right = sortedListToBST1(slow.next); root = new TreeNode(slow.val);
root.left = left;
root.right = right; return root;
}
SOLUTION 2:
public TreeNode sortedListToBST(ListNode head) {
if (head == null) {
return null;
} int size = ;
ListNode cur = head;
while (cur != null) {
size++;
cur = cur.next;
} CurrNode curNode = new CurrNode(head);
return sortedListToBSTHelp(curNode, size);
} public class CurrNode {
ListNode node; CurrNode(ListNode node) {
this.node = node;
}
} // when the recursion is done, the curr node should point to the node
// which is the next of the block.
public TreeNode sortedListToBSTHelp(CurrNode curr, int size) {
if (size <= ) {
return null;
} TreeNode left = sortedListToBSTHelp(curr, size/); // because we want to deal with the right block.
TreeNode root = new TreeNode(curr.node.val);
curr.node = curr.node.next; TreeNode right = sortedListToBSTHelp(curr, size - - size/); root.left = left;
root.right = right; return root;
}
SOLUTION 3:
并且这个dfs有一个作用 会把指针移动到这个要建的树的下一个位置
经过这一行 cur就移动到了中间
我们建立 一个根 TreeNode root = new TreeNode(curNode.val);
把cur移动到下一个 curNode = curNode.next;
再用递归建立右树
root.right = right;
/**
* 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 {
ListNode curNode = null; public TreeNode sortedListToBST(ListNode head) {
if (head == null) {
return null;
} int size = ;
ListNode cur = head;
while (cur != null) {
size++;
cur = cur.next;
} curNode = head;
return dfs(head, size);
} // Use the size to control.
public TreeNode dfs(ListNode head, int size) {
if (size <= ) {
return null;
} TreeNode left = dfs(head, size / );
TreeNode root = new TreeNode(curNode.val); // move the current node to the next place.
curNode = curNode.next;
TreeNode right = dfs(curNode, size - size / - ); root.left = left;
root.right = right; return root;
}
}
LeetCode: Convert Sorted List to Binary Search Tree 解题报告的更多相关文章
- 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】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告
原题地址: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目内容: Given a sing ...
- 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 ...
- 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 ...
随机推荐
- FFMpeg写MP4文件例子分析
http://blog.csdn.net/eightdegree/article/details/7425811 这段时间看了FFMpeg提供的例子muxing.c,我略微修改了下源代码,使其生成一个 ...
- Unity3D引用dll打包发布的问题及解决
今年我们开始使用Unity3D开发MMORPG,脚本语言使用C#,这样我们就可以使用以往积累的许多类库.但是,在U3D中使用.NET dll的过程并不是那么顺利,比如我们今天遇到的这种问题. 一.问题 ...
- EL表达式的操作符
表9.1 EL表达式的操作符 操作符 功能和作用 . 访问一个bean属性或者Map entry [] 访问一个数组或者链表元素 () 对子表达式分组,用来改变赋值顺序 ? : 条件语句,比如:条件? ...
- Kali Linux Web 渗透测试视频教程— 第二课 google hack 实战
Kali Linux Web 渗透测试— 第二课 代理简介 文/玄魂 课程地址: http://edu.51cto.com/course/course_id-1887.html 目录 shellKal ...
- jQuery的XX如何实现?——2.show与链式调用
往期回顾: jQuery的XX如何实现?——1.框架 -------------------------- 源码链接:内附实例代码 jQuery使用许久了,但是有一些API的实现实在想不通.于是抽空看 ...
- [BTS] Exception occurred when persisting state to the database
Error 1 Uncaught exception (see the 'inner exception' below) has suspended an instance of service 'J ...
- paip.解决中文url路径的问题图片文件不能显示
paip.解决中文url路径的问题图片文件不能显示 #现状..中文url路径 图片文件不能显示 <img src="img/QQ截图20140401175433.jpg" w ...
- 修改Oracle并行度的方法
Oracle并行度默认为1,适当修改并行度对提高性能有很大帮助 1.查看并行度 select table_name,degree from user_tables; --并行度按照用户表分别设置 2. ...
- 整站HTTPS后的跨域请求 CORS是否还有效?
| 导语 手Q马上就要全量https了,很多业务都有跨域ajax请求的需求,原来使用的CORS头在HTTPS环境中还继续能用吗?我搜遍了谷歌.百度,都没看到有明确的答案,那么就自己来尝试一下吧. 关 ...
- Linux中如何产生core文件?
在程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息).使用gdb来查看core文件,可以指示出导致程序出错的代码所在文件和行数. 1.core文件 ...