- 博客(20)
- 资源 (6)
- 收藏
- 关注
原创 LeetCode题解:Sum Root to Leaf Numbers
Given 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 represents the number 123.Find the total sum of al
2015-10-02 21:14:31 803 1
原创 LeetCode题解:Word Ladder
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a time
2015-10-02 21:13:10 1084
原创 LeetCode题解:Best Time to Buy and Sell Stock II
Say 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 as many transactions as you like (ie, buy one and
2015-10-02 21:07:59 925
原创 LeetCode题解:Best Time to Buy and Sell Stock
Say 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, buy one and sell one share of the stock), desi
2015-10-02 21:06:16 1048
原创 LeetCode题解:Triangle
Given 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 [ [2], [3,4], [6,5,
2015-10-02 21:04:26 880
原创 LeetCode题解:Populating Next Right Pointers in Each Node
Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node,
2015-10-02 21:01:47 918
原创 LeetCode题解:Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3
2015-10-02 20:59:45 786
原创 LeetCode题解:Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.For example: Given the below binary tree and sum = 22, 5 / \
2015-10-02 20:50:41 721
原创 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 balanced BST.题意:给定一个升序排列的单链表,转换为二叉搜索树解决思路:同样是二分……代码:public class Solution { private ListNode node;
2015-10-02 20:45:44 988
原创 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.题意:给定一个升序数组,将它转换为二叉搜索树解决思路:二分……代码:public class Solution { public TreeNode sortedArrayToBST(int[] num
2015-10-02 20:43:08 555
原创 LeetCode题解:Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.题解:通过中序遍历和后序遍历还原二叉树解决思路:首先要明确一点,对于后序遍历的结果,如果一个元素所在的位置为i,若在中序遍历的
2015-10-02 20:35:46 772
原创 LeetCode题解:Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.题意:给定一棵树的前序遍历和中序遍历,还原二叉树解决思路:我们可以知道的是,前序遍历的第一个结点就是根节点,而且对于中序遍历,每
2015-10-02 20:16:29 485
原创 LeetCode题解:Binary Tree Zigzag Level Order Traversal
Given 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 and alternate between).For example: Given binary tree
2015-10-02 20:10:01 788
原创 LeetCode题解:Validate Binary Search Tree
Given 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 less than the node’s key. The right
2015-10-02 20:06:23 674
原创 LeetCode题解:Unique Binary Search Trees II
Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below.1 3 3 2
2015-10-02 20:01:33 678
原创 LeetCode题解:Unique Binary Search Trees
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example, Given n = 3, there are a total of 5 unique BST’s.1 3 3 2 1 \ /
2015-10-02 19:57:15 512
原创 LeetCode题解:Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2].题意:中序遍历二叉树解决思路:”左根右“的递归或者通过栈完成的迭代代码:p
2015-10-02 19:29:46 640
原创 LeetCode题解:Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example: Given “25525511135”,return [“255.255.11.135”, “255.255.111.35”]. (Order does not
2015-10-02 19:27:50 882
原创 LeetCode题解:Reverse Linked List II
Reverse 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 = 4,return 1->4->3->2->5->NULL.Note: Given m, n satisfy the following co
2015-10-02 19:26:17 780
原创 LeetCode题解:Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:‘A’ -> 1 ‘B’ -> 2 … ‘Z’ -> 26 Given an encoded message containing digits, determine the total number of
2015-10-02 19:19:42 1729
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人