摘要: 题目: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:We can solve this easily using recursion. Why? Because each of the leftChild and rightChild of a node is a sub-tree itself. We first co 阅读全文
posted @ 2013-04-30 11:59 tanghulu321 阅读(109) 评论(0) 推荐(0) 编辑
摘要: 题目:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.思路:与N queens 一样代码:class Solution {public: bool isOK(vector<int> oneSolution, int k, int val){ for (int i=0; i<k; i++){ if (oneSol... 阅读全文
posted @ 2013-04-30 10:19 tanghulu321 阅读(83) 评论(1) 推荐(0) 编辑
摘要: 题目:You are climbing a stair case. It takesnsteps 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?思路: 此题可以由迭代或DP解决,在此贴一个 top down的DP代码:class Solution {public: int helper(int n, vector<int> &map){ if (n < 0) retu... 阅读全文
posted @ 2013-04-30 07:40 tanghulu321 阅读(155) 评论(0) 推荐(0) 编辑