[LeetCode] Word Ladder
Word LadderGiven two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter ...
[LeetCode] Sum Root to Leaf Numbers
Sum Root to Leaf NumbersGiven a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which...
[LeetCode] Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock IISay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete...
[LeetCode] Best Time to Buy and Sell Stock
Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie,...
[LeetCode] Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock IIISay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complet...
[LeetCode] Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solutio...
[LeetCode] Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each NodeGiven a binary tree struct TreeLinkNode { TreeLinkNode *left; &...
[LeetCode] Triangle
TriangleGiven a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ ...
[LeetCode] Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked ListGiven a binary tree, flatten it to a linked list in-place.For example,Given 1 &nbs...
[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder TraversalGiven preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.解题思路:题意为...
[LeetCode] Convert Sorted List to Binary Search Tree
Convert Sorted List to Binary Search TreeGiven a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.解题思路:题意为构造有序链表的二分查找树。找到中间节点的办法用双指针法。注意我们还需要保存中间节点的...
[LeetCode] Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.解题思路:题意为构建有序数组的二分查找树。比较简单,用递归方法即可,中间的元素作为根节点,前半部分作为左孩子树,右半部分作...
[LeetCode] Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order TraversalGiven a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level ...
[LeetCode] Validate Binary Search Tree
Validate Binary Search TreeGiven a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys les...
[LeetCode] Unique Binary Search Trees II
Unique Binary Search Trees IIGiven n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should ret...
[LeetCode] Binary Tree Inorder Traversal
Binary Tree Inorder TraversalGiven a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 &n...
[LeetCode] Partition List
Partition ListGiven a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative...
[LeetCode] Reverse Linked List II
Reverse Linked List IIReverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n&nb...
[LeetCode] Subsets II
Subsets IIGiven a collection of integers that might contain duplicates, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not conta...
[LeetCode] Gray Code
Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print t...