上一页 1 ··· 3 4 5 6 7 8 下一页
摘要: 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 阅读(171) 评论(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 阅读(249) 评论(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 阅读(149) 评论(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 阅读(151) 评论(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 阅读(145) 评论(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 阅读(111) 评论(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 阅读(159) 评论(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 阅读(156) 评论(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 阅读(118) 评论(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 阅读(126) 评论(0) 推荐(0) 编辑
上一页 1 ··· 3 4 5 6 7 8 下一页