摘要:如果对BST 中序遍历, 会产生有序数列。 非递归法: 二刷, 根据二叉树原理进行递归:
阅读全文
摘要:Recursion: 时间O(n) 空间O(h) 因为要找最长的连续路径,我们在遍历树的时候需要两个信息,一是目前连起来的路径有多长,二是目前路径的上一个节点的值。我们通过递归把这些信息代入,然后通过返回值返回一个最大的就行了。
阅读全文
摘要:那么这道题给的提示是让我们用BST的性质来解题,最重要的性质是就是左<根<右,那么如果用中序遍历所有的节点就会得到一个有序数组。所以解题的关键还是中序遍历啊。
阅读全文
摘要:此题的思路是运用hashmap和double linked nodes The hash makes the time of get() to be O(1). The list of double linked nodes make the nodes adding/removal operati
阅读全文
摘要:题目要求time complexity 是O(log (m+n)), 先归并排序也是不可行的, 时间复杂度为O(m+n)
阅读全文
摘要:Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord,
阅读全文
摘要:Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain lea
阅读全文
摘要:Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Example 2: Note: Try to come up as many solutions as you
阅读全文
摘要:Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8,
阅读全文
摘要:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Example 2: 复杂度 时间 O(NM) 空间 O(1) 思路
阅读全文
摘要:Given a stack of integers, sort it in ascending order using another temporary stack. Examples:
阅读全文
摘要:Find the sum of all left leaves in a given binary tree. Example:
阅读全文