Problem Description: http://oj.leetcode.com/problems/path-sum/

Pretty easy.

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> getSums(TreeNode *root) {
vector<int> sums; if(root->left == NULL && root->right == NULL)
sums.push_back(root->val); if(root->left != NULL){
vector<int> left_sums;
left_sums = getSums(root->left);
for(auto item : left_sums) {
sums.push_back(item + root->val);
}
} if(root -> right != NULL) {
vector<int> tmp_sums;
tmp_sums = getSums(root->right);
for(auto item : tmp_sums) {
sums.push_back(item + root->val);
}
} return sums;
} bool hasPathSum(TreeNode *root, int sum) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(root == NULL)
return false; vector<int> sums = getSums(root); for(auto item : sums) {
if(item == sum)
return true;
} return false;
}
};

Path Sum [LeetCode]的更多相关文章

  1. 【LeetCode】Path Sum ---------LeetCode java 小结

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. Binary Tree Maximum Path Sum leetcode java

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  3. Binary Tree Maximum Path Sum - LeetCode

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  4. Minimum Path Sum [LeetCode]

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. Minimum Path Sum——LeetCode

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  6. Minimum Path Sum leetcode java

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  7. Path Sum leetcode java

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  8. Binary Tree Maximum Path Sum [leetcode] dp

    a(i):在节点i由于单边路径的最大结束 b(i):在节点i路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->righ ...

  9. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

随机推荐

  1. SQL查看一张表中是否存在记录

    今天在QQ群众讨论到一个问题,记录下下来,一边以后用的时候可以翻阅 总结除了三种方法 --方法1,,这一种方法不行,,错误的认识了,@@ROWCOUNT,,,唉,,学艺不精,,丢人啊 SELECT T ...

  2. PCL点云库:ICP算法

    ICP(Iterative Closest Point迭代最近点)算法是一种点集对点集配准方法.在VTK.PCL.MRPT.MeshLab等C++库或软件中都有实现,可以参见维基百科中的ICP Alg ...

  3. 如何在iOS 7.0中隐藏状态栏

    使用Cordova做了一个小项目,在原来iOS6的时候显示挺好,升级为iOS7后,每次App启动后都会显示状态栏,而且状态栏和App的标题栏重叠在一起,非常难看,因此需要将状态栏隐藏起来.   首先, ...

  4. CSS选择器、优先级与匹配原理(转)

    CSS选择器.优先级与匹配原理 导航 为了分析Bootstrap源码,所以的先把CSS选择器相关的东东给巩固好 废话就不多说了 CSS 2.1 selectors, Part 1 计算指定选择器的优先 ...

  5. [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)

    题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...

  6. [POJ1830]开关问题(高斯消元,异或方程组)

    题目链接:http://poj.org/problem?id=1830 题意:中文题面,求的是方案数. 首先可以知道, 如果方案数不止一个的话,说明矩阵行列式值为0,即存在自由变元,由于变量只有两种状 ...

  7. [转]What you need to know about transimpedance amplifiers – part 1

    Transimpedance amplifiers (TIAs) act as front-end amplifiers for optical sensors such as photodiodes ...

  8. linux挂载共享文件夹

    挂载windows共享目录或FTP: 方式一:包含密码 Shell代码 收藏代码 sudo mount //192.168.10.22/FTPServer /windows -o username=u ...

  9. Linux c 下使用getopt()函数

    命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...

  10. HBase介绍及简易安装(转)

    HBase介绍及简易安装(转) HBase简介 HBase是Apache Hadoop的数据库,能够对大型数据提供随机.实时的读写访问,是Google的BigTable的开源实现.HBase的目标是存 ...