02 2016 档案

摘要:complete binary tree:除最后一行外每一行的节点都有两个儿子,最后一行的节点尽可能靠左。 ver0: 1 class Solution { 2 public: 3 int countNodes(TreeNode* root) { 4 if(!root) return 0; 5 re 阅读全文
posted @ 2016-02-24 17:24 co0oder 阅读(195) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 void flatten(TreeNode* root) { 4 if(!root) return; 5 flatten(root->left); 6 flatten(root->right); 7 8 TreeNode* right = 阅读全文
posted @ 2016-02-24 16:40 co0oder 阅读(264) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 4 if(root->val >= p->val && root->val <= q-> 阅读全文
posted @ 2016-02-24 16:17 co0oder 阅读(153) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 int rank; 4 int result; 5 6 void help(TreeNode* root, int k){ 7 if(!root) return; 8 9 help(root->left, k); 10 if(++rank 阅读全文
posted @ 2016-02-24 16:04 co0oder 阅读(158) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2016-02-24 15:59 co0oder 阅读(151) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 int minDepth(TreeNode* root) { 4 if(root == NULL) return 0; 5 if(root->left == NULL || root->right == NULL) return 1 + 阅读全文
posted @ 2016-02-23 23:09 co0oder 阅读(114) 评论(0) 推荐(0)
摘要:Given the following perfect binary tree, 1 / \ 2 3 / \ / \ 4 5 6 7 After calling your function, the tree should look like: 1 -> NULL / \ 2 -> 3 -> NUL 阅读全文
posted @ 2016-02-18 00:24 co0oder 阅读(174) 评论(0) 推荐(0)
摘要:是否存在根到叶节点的和等于给定数。 思路:递归。判断左儿子或右儿子是否存在这一路径(sum变为sum-root->val)。 ver0:递归到最后会是叶节点的左右儿子,所以要有root==NULL的判断 1 class Solution { 2 public: 3 bool hasPathSum(T 阅读全文
posted @ 2016-02-18 00:01 co0oder 阅读(170) 评论(0) 推荐(0)
摘要:(0,0)到(m-1,n-1),只能向下或向右。求走法的个数。 整个路径必定要m-1向下,n-1向右,即m-1+n-1个空位中选出m-1个进行向下,即Cm-1m+n-2,此法待实现。 1 class Solution { 2 public: 3 int uniquePaths(int m, int 阅读全文
posted @ 2016-02-14 00:13 co0oder 阅读(124) 评论(0) 推荐(0)
摘要:只能用1、2相加得到n,求有几种加法。 ver0:递归,意料之中的TLE 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 if(n==1) return 1; 5 if(n==2) return 2; 6 return climbS 阅读全文
posted @ 2016-02-13 22:45 co0oder 阅读(135) 评论(0) 推荐(0)
摘要:求数组nums[i,j]的和 思路:另开一sum数组,sum[i]为nums[0,i]的和,所以nums[i,j] = sum[j] - sum[i-1] 1 class NumArray { 2 public: 3 vector<int> sum; 4 NumArray(vector<int> & 阅读全文
posted @ 2016-02-13 22:05 co0oder 阅读(154) 评论(0) 推荐(0)
摘要:一条街上若干住户,给出财产,不能抢劫相邻的两家,求最多抢得的钱数。 核心思想:f(n)=Max{f(n−1),f(n−2)+An} 1 class Solution { 2 public: 3 int rob(vector<int>& nums) { 4 if(nums.empty()) retur 阅读全文
posted @ 2016-02-13 21:39 co0oder 阅读(222) 评论(0) 推荐(0)
摘要:Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 1 class Solution { 2 public: 3 int numTrees(int n) { 4 int 阅读全文
posted @ 2016-02-12 14:56 co0oder 阅读(166) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2016-02-12 14:50 co0oder 阅读(383) 评论(0) 推荐(0)