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分组查询每组前几条数据

    /*第一种实现方法,效率低并且有错误*/ DECLARE @DD DATETIME SET @DD = GETDATE() SELECT a.GoodsID , a.Account , a.LastU ...

  2. 【转载】COM多线程原理与应用

    原文:COM多线程原理与应用 目录: COM多线程原理与应用 目录: 前言: 套间: 套间的定义: 套间的分类: 套间的进入和退出: 对象的同步: 组件对象的同步: COM对象线程模型: 进程内对象线 ...

  3. 大神写的一个纯CSS圆角框,膜拜!(支持IE9一下的低版本)

    留着提醒自己,底层才是最重要的,不要一直傻瓜的编程下去! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  4. RARP

    ARP的工作原理如下:1. 首先,每台主机都会在自己的ARP缓冲区 (ARP Cache)中建立一个 ARP列表,以表示IP地址和MAC地址的对应关系.2. 当源主机需要将一个数据包要发送到目的主机时 ...

  5. yum安装指定版本软件包__20160308

    举例子: 安装 libGL-devel 1. yum list libGL-devel 居然说没有匹配的包的信息... [root@CentOS6 ~]# yum list libGL-devel L ...

  6. bootstrap 固定定位

    <!DOCTYPE html> <html><head><meta charset="UTF-8"><title>Boo ...

  7. Tuning 简介

    典型的不好的设计: 破坏了系统的可扩展性(韧性) Applications requiring significant concurrency management as user populatio ...

  8. thinkphp中ajaxReturn方法实现ajax效果

    前台代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. maven的仓库、生命周期与插件

    一.仓库 统一存储所有Maven项目共享的构建的位置就是仓库. 仓库分为本地仓库和远程仓库.远程仓库又分为中央仓库(中央仓库是Maven核心自带的远程仓库),伺服(另一种特殊的远程仓库,为节省宽带和时 ...

  10. Android AIDL Service

    AIDL Service //跨进程调用Service    一个APK想调用另一个APK中的Service 如何实现AIDL //定义两个进程之间的通信接口 Demo1 传递简单数据 AidlSer ...