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.

https://siddontang.gitbooks.io/leetcode-solution/content/tree/convert_sorted_listarray_to_binary_search_tree.html

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

http://blog.unieagle.net/2012/12/14/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Aconvert-sorted-list-to-binary-search-tree/

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)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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. 这道 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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.每次更新的 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. .NET Core采用的全新配置系统[5]: 聊聊默认支持的各种配置源[内存变量,环境变量和命令行参数]

    较之传统通过App.config和Web.config这两个XML文件承载的配置系统,.NET Core采用的这个全新的配置模型的最大一个优势就是针对多种不同配置源的支持.我们可以将内存变量.命令行参 ...

  2. ASP.NET Core的配置(5):配置的同步[设计篇]

    本节所谓的"配置同步"主要体现在两个方面:其一,如何监控配置源并在其变化的时候自动加载其数据,其目的是让应用中通过Configuration对象承载的配置与配置源的数据同步:其二. ...

  3. 【CSS进阶】试试酷炫的 3D 视角

    写这篇文章的缘由是因为看到了这个页面: 戳我看看(移动端页面,使用模拟器观看) 运用 CSS3 完成的 3D 视角,虽然有一些晕3D,但是使人置身于其中的交互体验感觉非常棒,运用在移动端制作一些 H5 ...

  4. 扫二维码下载apk并统计被扫描次数(及微信屏蔽下载解决方案)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5395715.html 需求:想让用户扫描一个二维码就能下载APP,并统计被扫描次数. 两种实现方法: 1.一 ...

  5. ORACLE 11gR2 DG(Physical Standby)日常维护01

    环境:RHEL 6.4 + Oracle 11.2.0.4 一.主备手工切换 1.1 主库,切换成备库并启动到mount 1.2 备库,切换成主库并启动到open 1.3 新的备库启动日志应用 二.重 ...

  6. 用php实现一个简单的链式操作

    最近在读<php核心技术与最佳实践>这本书,书中第一章提到用__call()方法可以实现一个简单的字符串链式操作,比如,下面这个过滤字符串然后再求长度的操作,一般要这么写: strlen( ...

  7. 利用Python进行数据分析(10) pandas基础: 处理缺失数据

      数据不完整在数据分析的过程中很常见. pandas使用浮点值NaN表示浮点和非浮点数组里的缺失数据. pandas使用isnull()和notnull()函数来判断缺失情况. 对于缺失数据一般处理 ...

  8. 给jquery-validation插件添加控件的验证回调方法

    jquery-validation.js在前端验证中使用起来非常方便,提供的功能基本上能满足大部分验证需求,例如:1.内置了很多常用的验证方法:2.可以自定义错误显示信息:3.可以自定义错误显示位置: ...

  9. Android ORM -- Litepal(1)

    ORM,即Object Relation Mapping,对象关系映射,实现了程序里面的类和数据库里面的数据之间的对应关系,对数据库的操作可以通过对类的操作去实现,不用再写SQL语句,从而提高了开发效 ...

  10. spring和struts2的整合的xml代码

    导入spring的pring-framework-4.0.4.RELEASE的所有包,导入struts2下(对于初学的推荐)bin下所有的包,虽然有些包可以能现在你用不到,但可以保证你基本上不会出现缺 ...