Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015
学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些.
先做一个例子:
9/22/2015
Go over one example to build some muscle memory about this bottom up, O(1) solution to find the root node in subtree function.
Sorted List:
1->2->3->4->5->6->7,
How to convert the above sorted list to a binary search tree?





Here is the whole story for working on this alogrithm problem:
109 Convert sorted list to binary search tree (No. 109)
8/25/2015
Read the following blogs:
C#, bottom up, time O(n), space O(log n) solution - best solution:
C#, top down, time O(n^2), space O(long n) solution - naive solution:
worked on code 2 times, first time, the calculation is kind of messy, then, worked on Leetcode question 108, get the idea to make it more simple;
tips like len/2 only shows once, afterwards, use m instead. Just need to improve coding, think to make it more abstract, simple.
9/21/2015
Review the best solution, and then, totally forgot the bottom up solution idea. So, update the code with more comment.
Need to review more about bottom up/ top down solution in tree problems. Get more experience on bottom-up solution, read some articles about it.
9/22/2015
Go over one example to build some muscle memory about this bottom up, O(1) solution to find the root node in subtree function.
Sorted List:
1->2->3->4->5->6->7,
How to convert the above sorted list to a binary search tree?
Thought process:
1. First, get length of the list, which is 7 in the above list;
2. Secondly, define a recursive function called
constructBST(ref TreeNode head, int start, int end)
the above function definition, 3 arguments:
1. First one is the reference of head node of sorted list,
2. Start index of the list,
3. End index of the list
In the function, first define the base case:
head is null, or start<end, return null
start==end, return head
then, call the recursive function for left subtree:
head node is the same, start is the same, but end is len/2-1;
great tip comes in, the head node should move in the function, so that
root node can be accessed in the list using O(1), instead of starting from very beginning.
One more statement:
head = head.next;
TreeNode root = head; // return this node as tree root node
Root.left = left subtree root node
Root.right = right subtree recursive function
constructBST(ref head.next, mid+1, end)
The tips to remember in the design, the recursive function should return the root node of the tree; secondly, input argument of linked list should
use reference, and also head node moves in the recursive function, so it is O(1) to find the root node.
Just cannot believe that only call .next function once in the recursive function! How to argue that the move is only once? Therefore, the total calls
of .next should be length of list. Total recursive function calls is n, length of list.
Debate why the recursive function has to return root node, and set up root node, connect its left/ right subtree root node.
Debate why the linked list head node is moving.
设计这个递归函数, 如何避免从链的头开始访问, 到中间点? 最关键是让链的头移动, 当需要设计树的根节点, 只要移动一步, 就是根节点.
画一个图, 帮助自己理解记忆; 看一篇文章, 开拓思路.
The main point to understand the best solution using O(ln N) space, the left subtree has to be built first, and then,
head node can be retrieved as .next method call, root node can be set up, and then, left subtree can be built. So,
left subtree, then right subtree, then the tree with root node. Bottom up.
Whereas sorted array to BST, the root node can be find right away, and then, tree can be set up top down. Julia is
still confusing this top down / bottom up difference. :-) read more blogs.
Blogs:
1. use global variable to remember the head node of linked list, great idea:
http://www.jiuzhang.com/solutions/convert-sorted-list-to-binary-search-tree/
2. another implementation using two pointers. Try it using C# later.
3. Java implementation, teach me how to use reference in Java or wrapper class. Good point!
http://rleetcode.blogspot.ca/2014/02/convert-sorted-list-to-binary-search.html
4. Time and space complexity analysis - think about it - very clear analysis
5. Three implementation discussions
http://www.cnblogs.com/feiling/p/3267917.html
6. Great explanation - bottom up / top down, and in order traversal/ post order traversal
https://leetcodenotes.wordpress.com/2013/11/23/convert-sorted-list-to-binary-search-tree/
Implement the above 6 blogs using C#, and then, share the blog. After 1 month, check again and see if I can come out the bottom
up solution in 5 minutes. If yes, stop; otherwise review again.
Leetcode: Convert sorted list to binary search tree (No. 109)的更多相关文章
- 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 将有序链表转为二叉搜索树
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 108. Convert Sorted Array to Binary Search Tree 、109. Convert Sorted List to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 这个题使用二分查找,主要要注意边界条件. 如果left > right,就返回NULL.每次更新的 ...
- 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 DFS,深度搜索
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
随机推荐
- 【Win 10应用开发】延迟加载图片的另一种方法
上一篇文章中老周给大伙介绍了x:Phase和x:Bind的用法,并演示了一个延迟加载的示例.不过,那个例子会遗留一个问题,就是UI线程被阻塞,所以启动应用较慢. 如果希望图片可以延迟加载,或许我们可以 ...
- 难道.NET Core到R2连中文编码都不支持吗?
今天写了一个简单的.NET Core RC2控制台程序,发现中文显示一直是乱码.查看操作系统设置,没有问题:查看源文件编码,也没有问题:甚至查看了Console字符编码相关的注册表,依然没有发现问题. ...
- SQL Server-外部联接基础回顾(十三)
前言 本节我们继续讲讲联接类型中的外部联接,本节之后我们将讲述有关联接性能以及更深入的知识,简短内容,深入的理解,Always to review the basics. 外部联接 外部联接又分为左外 ...
- c 网络与套接字socket
我们已经知道如何使用I/O与文件通信,还知道了如何让同一计算机上的两个进程进行通信,这篇文章将创建具有服务器和客户端功能的程序 互联网中大部分的底层网络代码都是用C语言写的. 网络程序通常有两部分组成 ...
- 【NLP】揭秘马尔可夫模型神秘面纱系列文章(二)
马尔可夫模型与隐马尔可夫模型 作者:白宁超 2016年7月11日15:31:11 摘要:最早接触马尔可夫模型的定义源于吴军先生<数学之美>一书,起初觉得深奥难懂且无什么用场.直到学习自然语 ...
- TFS2017持续集成构建
TFS2017发布已经有几个月了,经过了几天的部署和尝试,TFS2017的功能变化真是挺大的.特别是在构建方面的变化,在产品的向导中已经声明XAML版本控制器和代理已经弃用了,并建议升级原来13和15 ...
- 用eclipse开发项目时遇到的常见错误整理,和配套解决方案(1)
01. MyEclipse项目导入eclipse后,怎么发布不了? 今天导入了之前的一个MyEclipse项目,更改jdk后,发现发布不了.解决方案如下: 打开项目根目录,找到.settings文件夹 ...
- HTML基本元素(四)
1.HTML框架 框架的作用就是把浏览器窗口划分成多个子窗口,而且每个子窗口都可以载入各自的HTML文档. *注意:html框架集与body同级,因此不能同时出现! 框架结构标签:<frames ...
- JavaScript jQuery 中定义数组与操作及jquery数组操作
首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...
- 【Asp.Net Core】一、Visual Studio 2015 和 .NET Core 安装
安装 Visual Studio 和 .NET Core 1.安装 Visual Studio Community 2015,选择 Community 下载并执行默认安装.Visual Studio ...