CoderJesse  
wangjiexi@CS.PKU

2013年3月1日

摘要: Given 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 order of the nodes in each of the two partitions.For example,Given 1->4->3->2->5->2 and x = 3,return 1->2->2-> 阅读全文
posted @ 2013-03-01 14:13 CoderJesse 阅读(100) 评论(0) 推荐(0)
 
摘要: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr eat / \ / \g r e at / \ a tTo scramble the string, we may choose any non-lea... 阅读全文
posted @ 2013-03-01 14:12 CoderJesse 阅读(230) 评论(0) 推荐(0)
 
摘要: Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.归并排序。 1 class Solution { 2 public: 3 void merge(int A[], int m, int B... 阅读全文
posted @ 2013-03-01 14:04 CoderJesse 阅读(116) 评论(0) 推荐(0)
 
摘要: The 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 the sequence of gray code. A gray code sequence must begin with 0.For example, given n = 2, return [0,1,3,2]. Its gray c 阅读全文
posted @ 2013-03-01 14:03 CoderJesse 阅读(121) 评论(0) 推荐(0)
 
摘要: A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message "12", it 阅读全文
posted @ 2013-03-01 14:02 CoderJesse 阅读(161) 评论(0) 推荐(0)
 
摘要: Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,If S = [1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]每个元素都有两种选择,... 阅读全文
posted @ 2013-03-01 14:01 CoderJesse 阅读(129) 评论(0) 推荐(0)
 
摘要: 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 condition:1 <= m <= n <= length of list.注意题目Do it in-place and in 阅读全文
posted @ 2013-03-01 13:58 CoderJesse 阅读(115) 评论(0) 推荐(0)
 
摘要: 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 matter)深度优先搜索吧。注意剪枝的简单条件。还有注意ip的规则!class Solution {public: vector<st 阅读全文
posted @ 2013-03-01 13:56 CoderJesse 阅读(266) 评论(0) 推荐(0)
 
摘要: Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].不用解释。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), ... 阅读全文
posted @ 2013-03-01 13:54 CoderJesse 阅读(116) 评论(0) 推荐(0)
 
摘要: 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 1 \ / / / \ \ 3 2 1 1 3 2 / / ... 阅读全文
posted @ 2013-03-01 13:53 CoderJesse 阅读(136) 评论(0) 推荐(0)
 
摘要: 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 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 ... 阅读全文
posted @ 2013-03-01 13:49 CoderJesse 阅读(131) 评论(0) 推荐(0)
 
摘要: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false.动规题。用A[i1][i2]表示s1中的前i1个元素和s2中前i2个元素能否构成s3的前i1+i3个元素。A[i1][i2] = ( 阅读全文
posted @ 2013-03-01 13:48 CoderJesse 阅读(135) 评论(0) 推荐(0)
 
摘要: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?利用中序周游从小到大的顺序检查,找到第一个异常元素。再利用中序周游从大到小的顺序检查,找到第一个异常元素。将这两个元素交换即可。 1 class Solution 阅读全文
posted @ 2013-03-01 13:37 CoderJesse 阅读(121) 评论(0) 推荐(0)
 
摘要: 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 subtree of a node contains only nodes with keys greater than the node's key.Both the left an 阅读全文
posted @ 2013-03-01 13:34 CoderJesse 阅读(115) 评论(0) 推荐(0)
 
摘要: Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.不用解释吧。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * ... 阅读全文
posted @ 2013-03-01 13:33 CoderJesse 阅读(107) 评论(0) 推荐(0)
 
摘要: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and iterati... 阅读全文
posted @ 2013-03-01 13:32 CoderJesse 阅读(129) 评论(0) 推荐(0)
 
摘要: 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 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3... 阅读全文
posted @ 2013-03-01 13:26 CoderJesse 阅读(137) 评论(0) 推荐(0)
 
摘要: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.基本不用解释吧。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 ... 阅读全文
posted @ 2013-03-01 13:21 CoderJesse 阅读(141) 评论(0) 推荐(0)
 
摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.首先,前序周游序列的最后一个元素是根节点。那么需要在中序周游序列中找到这个元素的下标,这样就可以将两个序列分别分割开,用同样的方法找根节点的左右子节点。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * ... 阅读全文
posted @ 2013-03-01 13:20 CoderJesse 阅读(125) 评论(0) 推荐(0)
 
摘要: Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.首先,后序周游序列的最后一个元素是根节点。那么需要在中序周游序列中找到这个元素的下标,这样就可以将两个序列分别分割开,用同样的方法找根节点的左右子节点。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * ... 阅读全文
posted @ 2013-03-01 13:17 CoderJesse 阅读(149) 评论(0) 推荐(0)
 
摘要: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]偷... 阅读全文
posted @ 2013-03-01 13:12 CoderJesse 阅读(110) 评论(0) 推荐(0)
 
摘要: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]二叉树的广度优先周游,稍稍做一下处理。 1 class Solution { ... 阅读全文
posted @ 2013-03-01 13:09 CoderJesse 阅读(109) 评论(0) 推荐(0)
 
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这个问题比较容易想到O(nlogn)的方法,通过链表的计数来找到一个序列的中间节点。不过,存在一个更加高效的方法,可以优化到O(n)。 这个方法有点不容易想到。之前做过的用排好序的数组元素构造balanced BST,是通过下表找中间元素,从上到下建立树。通过链表找中间元素只能通过计数。但如果可以有一种方法可以只经历一次遍历链表,合理利用当前的计数值,就可以安排好各个节点分.. 阅读全文
posted @ 2013-03-01 13:07 CoderJesse 阅读(240) 评论(0) 推荐(0)
 
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.首先,对于一个序列,找到它的中间元素作为根节点。根节点的左子节点连接到用同样方法处理中间元素之前的序列得到的根节点,根节点的右子节点连接到用同样方法处理中间元素之后的序列得到的根节点。时间复杂度为O(n)。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *l... 阅读全文
posted @ 2013-03-01 12:53 CoderJesse 阅读(120) 评论(0) 推荐(0)
 
摘要: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.思路很简单,对于每个节点,递归求得其左右子节点的高度。由此可以得到它是否是平衡二叉树。对于其左右子节点用同样的方法递归。当所有的节点都是平衡二叉树的时候,那么整棵树才是平衡二 阅读全文
posted @ 2013-03-01 12:46 CoderJesse 阅读(119) 评论(0) 推荐(0)
 
摘要: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.周游二叉树即可,很简单。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right;... 阅读全文
posted @ 2013-03-01 12:41 CoderJesse 阅读(126) 评论(0) 推荐(0)
 
摘要: 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 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2... 阅读全文
posted @ 2013-03-01 12:39 CoderJesse 阅读(160) 评论(0) 推荐(0)
 
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
posted @ 2013-03-01 12:35 CoderJesse 阅读(139) 评论(0) 推荐(0)