摘要:
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... 阅读全文
摘要:
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... 阅读全文
摘要:
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)... 阅读全文
摘要:
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 ... 阅读全文
摘要:
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 ') 阅读全文
摘要:
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 ... 阅读全文
摘要:
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... 阅读全文
摘要:
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 ... 阅读全文
摘要:
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/... 阅读全文