摘要: 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 class Solution { 2 public: 3 int maxDepth(TreeNode *root) { 4 if(!root) { 5 return 0; 6 } 7 int dep... 阅读全文
posted @ 2014-04-04 22:25 beehard 阅读(105) 评论(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 iteratively. 1 class Solutio... 阅读全文
posted @ 2014-04-04 22:15 beehard 阅读(97) 评论(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.Solution: recursion.1 class Solution {2 public:3 bool isSameTree(TreeNode *p, TreeNode *q) {4 if(!p && !q)... 阅读全文
posted @ 2014-04-04 22:08 beehard 阅读(98) 评论(0) 推荐(0) 编辑
摘要: Write a function to find the longest common prefix string amongst an array of strings. 1 class Solution { 2 public: 3 string longestCommonPrefix(vector &strs) { 4 string res; 5 if(strs.empty()) 6 return res; 7 for(int i = 0; i < strs[0].size(); i++) { 8 ... 阅读全文
posted @ 2014-04-04 21:03 beehard 阅读(107) 评论(0) 推荐(0) 编辑
摘要: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"Solution: Place n left '(' and n right ')&# 阅读全文
posted @ 2014-04-04 20:45 beehard 阅读(148) 评论(0) 推荐(0) 编辑
摘要: 4SumGiven an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?Find all unique triplets in the array which gives the sum ... 阅读全文
posted @ 2014-04-04 10:30 beehard 阅读(146) 评论(0) 推荐(0) 编辑
摘要: 3SumClosestGiven an array S of n integers, find three integers in S such that the sum is closest toa given number, target. Return the sum of the three... 阅读全文
posted @ 2014-04-04 10:26 beehard 阅读(113) 评论(0) 推荐(0) 编辑
摘要: 3SumGiven an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?Find all unique triplets in the array which gives the sum ... 阅读全文
posted @ 2014-04-04 10:19 beehard 阅读(161) 评论(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 1 2 3 Solution: dp.http://fisherlei.blogspot.com/2013/03/... 阅读全文
posted @ 2014-04-04 08:34 beehard 阅读(145) 评论(0) 推荐(0) 编辑
摘要: You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Solution: Climd one step from last stair or climb 2 steps from the last last stair. 1 class Solution { 2 public: 3 int climbStairs(i... 阅读全文
posted @ 2014-03-29 02:26 beehard 阅读(111) 评论(0) 推荐(0) 编辑