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 ...
随机推荐
- Android 自定义View及其在布局文件中的使用示例
前言: 尽管Android已经为我们提供了一套丰富的控件,如:Button,ImageView,TextView,EditText等众多控件,但是,有时候在项目开发过程中,还是需要开发者自定义一些需要 ...
- jquery实现Tab选项卡菜单
效果图: 默认选中 科技 当鼠标悬停在 ...
- putty无密码登陆
1.打开puttygen.exe,点击Generate,然后按照说明用鼠标在空白处移动,生成密钥对. 2.保存私钥,不填passphrase.同时保存公钥,并打开公钥文件,将回车符去掉. 3.将公 ...
- 如何给现有的PDF文件添加页码
如何给现有的PDF文件添加页码 之前我写了如何打印PDF文件,有人qq问我怎样在打印时给PDF文件添加页码,的确,给PDF文件添加页码,可以帮助我们区分纸质档的PDF文件页面的先后顺序,方便我们对它的 ...
- 【知识积累】try-catch-finally+return总结
一.前言 对于找Java相关工作的读者而言,在笔试中肯定免不了遇到try-catch-finally + return的题型,需要面试这清楚返回值,这也是这篇博文产生的由来.本文将从字节码层面来解释为 ...
- sqlserver 游标的使用
declare @temp_temp uniqueidentifier--临时变量 DECLARE aaa CURSOR for select Id from A ------------------ ...
- C#--图片上传(PC端和APP)保存及 跨域上传说明
手动跨域操作文件 补录:跨域访问文件夹文件是一种常见的需求,下面主要介绍的的通过代码使用具有权限账号的人来达到跨域操作文件的能力. 现在补充一下普通的一些需求场景,今天就遇到了一种需要经常需要登录远程 ...
- 使用 GitHub 和 Hexo 搭建个人独立博客
Wordpress这类博客系统功能强大,可对与我只想划拉的写点东西的人,感觉大材小用了.而且wp需要部署,网站的服务器也会带来问题,国内的服务器首先需要备案,费用不低:国外服务器访问速度受影响.近 ...
- spring源码:IOC(li)
一.BeanDefinition BeanDefinition是配置文件<bean>元素标签在容器中内部表示形式.创建最终的BeanDefinition主要包括两个步骤: 1)利用Bean ...
- php注释规范
注释在写代码的过程中非常重要,好的注释能让你的代码读起来更轻松,在写代码的时候一定要注意注释的规范.(李昌辉) php里面常见的几种注释方式: 1.文件头的注释,介绍文件名,功能以及作者版本号等信息 ...