Sunday, February 22, 2015

[LeetCode]Unique BST


[LeetCode]Convert Sorted List to BST


[LeetCode]Convert Sorted Array to BST


[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal


[LeetCode]Construct Binary Tree from Inorder and Preorder Traversal


[LeetCode]Merge Interval


[LeetCode]Insert Interval


[LeetCode]LRU Cache


[LeetCode]Search for a Range


[LeetCode]Find Median in Two Sorted Array


[Data Structure]Union Find


[Data Structure]Trie


Tuesday, January 27, 2015

[LeetCode]Word Ladder


[LeetCode]Maximal Rectangle


[Algorithm]Maximum and Minimum in Window of Numbers


[Algorithm]Hill


[Algorithm]Reorder Array


[LeetCode]Best Time to Buy and Sell Stock IV


[LeetCode]Best Time to Buy and Sell Stock II


[LeetCode]Best Time to Buy and Sell Stock III


[LeetCode]Best Time to Buy and Sell Stock


[LintCode]Maximum Subarray III


[LintCode]Maximum Subarray Difference


[LintCode]Minimum Subarray



[LintCode]Maximum Subarray II


[LeetCode]Maximum Subarray


Sunday, January 18, 2015

[Algorithm]First Non Repeating Character


[Algorithm]Get Distance Between Two Nodes in Binary Tree


[LintCode]Lowest Common Ancestor


[Algorithm]Get Max Distance in Binary Tree


[Algorithm] Flatten Binary Tree to Doubly Linked List As Postorder


[Algorithm] Flatten Binary Tree to Doubly Linked List As Inorder


[Algorithm] Flatten Binary Tree to Doubly Linked List As Preorder


[LeetCode] Flatten Binary Tree to Linked List


[LeetCode]Longest Substring with At Most Two Distinct Characters


[LeetCode]Longest Substring Without Repeating Characters


[LeetCode]Minimum Window Substring


Sunday, January 11, 2015

[LeetCode]Partition List


[LeetCode]Merge Two Sorted Lists


[LeetCode]Remove Duplicates from Sorted List II


[LeetCode]Remove Duplicates from Sorted List


[LeetCode]Remove Nth Node From End of List


[LeetCode]Reorder List


[LeetCode]Reverse Linked List II


[LeetCode]Rotate List


[LeetCode]Sort List


[LeetCode]Swap Nodes in Pairs


[LeetCode]Sqrt


[LeetCode]Divide Two Integers

[LeetCode]Pow(x,n)


Wednesday, January 7, 2015

[LeetCode]Binary Zigzag Level Order Traversal


[LeetCode]Binary Tree Level Order Traversal II


[LeetCode]Binary Tree Level Order Traversal


[LintCode]Serialization and Deserialization Of Binary Tree

[Interview]Zillow Coding Test


[Data Structure]Binary Search Tree

本文主要涉及BST的构造以及order operation方面的内容:

[LintCode]Search Range In Binary Search Tree

首先是brute-force的方法, inorder看是不是在range里,在的话就加入。时间复杂度是O(N)。显然面试官不可能希望这个方法。

[LintCode]Remove Node in Binary Search Tree


删除的时候三种情况:
  • 没有子节点
  • 有一个子节点
  • 有两个子节点

[LintCode]Insert Node in Binary Search Tree

很简单题,每个node决定是往左还是往右还是就是这个节点。类似二分的做法。

[LeetCode]String to Integer(atoi)

主要麻烦的地方在于注意各种corner case,按照题目要求来就好。主要讲一下,判断overflow, 之前我用的是很蠢的判断溢出的方法,以正数为例,之前是Integer.MAX_VALUE减去十次原来的数,在减去新的一位看是不是大于0。刷第三次的时候觉得这个方法太过于愚蠢了,稍微想了下,只需要Integer.MAX_VALUE减去新的那一位再除以10,看是不是大于原来的数就可以了。

Monday, January 5, 2015

[LeetCode]Symmetric Tree

Top-down, 尾递归。

[LeetCode]Same Tree

Top-down, 尾递归。

[LeetCode]Validate Binary Search Tree

注意光判断左子结点小于当前节点右子结点大于当前节点是不够的。比如                  5
                                                                                                                                    /  \
                                                                                                                                  3    8
                                                                                                                                    \
                                                                                                                                     6
就满足条件但不是valid BST。要判断也应该是左子树最大值小于当前节点,右子树最小值大于当前节点。不过这样太麻烦,用top-bottom的方法,开始的范围是(-∞,+∞)。每次经过一个结点从(low, high)分割成(low, curr.val)和(curr.val, high)像两个子节点递归。不满足就返回false,碰到null返回true。

[LeetCode]Binary Search Tree Iterator

BST中序遍历的迭代器,Inorder遍历iterative版本的好处就体现出来了,我们可以控制停在哪里,而recursive版本的就不好控制。这里我们要决定的是如何找到后继结点,分两种情况,一种是有右子树,那么我们找到右子树最小值即可。第二种是没有右子树,我们pop出栈顶的元素就是后继结点,至于为什么,我们在Binary Tree Preorder Traversal已经证明过这个问题。最后注意一下终止条件即可。

[Algorithm]Binary Tree Morris Traversal

Binary Tree的Traversal问题总是面试的重点。不管是recursive还是iterative的版本,我们都需要额外的空间。

[LeetCode]Binary Tree Postorder Traversal

之前在Binary Tree Preorder Traversal中说过,preorder和inorder的方法不适于Postorder因为不方便模拟第三次对节点的访问,所以只好改变方法。

[LeetCode]Binary Tree Inorder Traversal

Binary Tree Preorder Traversal中说明过,Inorder的代码基本上是一样的,只有一行的区别。

[LeetCode]Binary Tree Preorder Traversal

BST遍历的问题,很经典的问题。