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 ...
随机推荐
- $(document).ready() 与window.onload的区别
1.执行时间 window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行. $(document).ready()是DOM结构绘制完毕后就执行,不必等到加载完毕. 2.编写个数不同 ...
- 【完全开源】微信客户端.NET版
目录 说明 功能 原理步骤 一些参考 说明 前两天比较闲,研究了一下web版微信.因为之前看过一篇博客讲微信web协议的,后来尝试分析了一下,半途中发现其实没什么意义,但又不想半途而废,所以最后做出了 ...
- .NET Core采用的全新配置系统[2]: 配置模型设计详解
在<.NET Core采用的全新配置系统[1]: 读取配置数据>中,我们通过实例的方式演示了几种典型的配置读取方式,其主要目的在于使读者朋友们从编程的角度对.NET Core的这个全新的配 ...
- 连接SQLServer时,因启用连接池导致孤立事务的原因分析和解决办法
本文出处:http://www.cnblogs.com/wy123/p/6110349.html 之前遇到过这么一种情况: 连接数据库的部分Session会出现不定时的阻塞,这种阻塞时长时短,有时候持 ...
- 3.C#面向对象基础聊天机器人
基于控制台的简单版的聊天机器人,词库可以自己添加. 聊天机器人1.0版本 源码如下: using System; using System.Collections.Generic; using Sys ...
- go语言注释
Go语言注释实例代码教程 - Go支持C语言风格的/* */块注释,也支持C++风格的//行注释. 当然,行注释更通用,块注释主要用于针对包的详细说明或者屏蔽大块的代码. 每个包都应有一个包注解,即 ...
- 存在即合理,重复轮子orm java版本
1,业务描述前序? 需求来源于,公司的运营部门.本人所在公司(私营,游戏行业公司),从初创业,我就进入公司,一直致力于服务器核心研发. 公司成立块3年了,前后出产了4款游戏,一直在重复的制造公司游戏对 ...
- 学习javascript数据结构(三)——集合
前言 总括: 本文讲解了数据结构中的[集合]概念,并使用javascript实现了集合. 原文博客地址:学习javascript数据结构(三)--集合 知乎专栏&&简书专题:前端进击者 ...
- Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】
原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...
- ASP.NET MVC搭建项目后台UI框架—11、自动加载下拉框查询
ASP.NET MVC搭建项目后台UI框架—1.后台主框架 需求:在查询记录的时候,输入第一个字,就自动把以这个字开头的相关记录查找出来,输入2个字就过滤以这两个子开头的记录,依次类推. 突然要用到这 ...