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. css参考文档; 官方英文说明!! 1 margin padding 百分比参照物 2 margin值为auto时的说明 3 div在div里垂直居中方法 4 dispaly:flex说明

    css参考文档        http://css.doyoe.com/ 两篇很好的文章:(下面的css官方英文说明链接 有时间可以研究下 http://www.w3.org/TR/css3-box/ ...

  2. MySQL(二) —— 数据类型与操作数据表

    数据类型 数据类型是指列.存储过程参数.表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型. 整型:TYNINT(-2^7 ~ 2^7-1); SMALLINT(-2^15 ~ ...

  3. git log 查看 提交历史

    在提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,可以使用 Git log 命令查看. 接下来的例子会用我专门用于演示的 simplegit 项目,运行下面的命令获取该项目源代码: git ...

  4. 序列化、反序列化(实体类或要序列化的对象类必须实现Serializable接口)

    package com.phone.shuyinghengxie; import java.io.Serializable; /* 一个类的对象要想序列化成功,必须满足两个条件: 该类必须实现 jav ...

  5. 屏幕序列Screen Sequences

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. 别名alias

    alias #查看已设置的别名 alias  别名='原命令' #暂时设定别名(重启失效):alias ls='ls --color=never' unalias  别名 #删除别名 设置别名永久生效 ...

  7. How much do we need to learn to be a Self-driving Car Engineer?

    Before everything we need programming skills in C++ and Python. One of the suggested book for C++ le ...

  8. kakfa的常用命令总结

    Kafka的版本间差异较大,下面是0.8.2.1的操作方法 首先cd到kafaka的bin目录下;   #step1启动zookeeper服务 nohup bin/zookeeper-server-s ...

  9. shift移动变量

    1.移动变量 脚本 sh05.sh #!/bin/bash # Program # Program shows the effect of shift function # History: # // ...

  10. iOS - Swift NSSize 尺寸

    前言 结构体,这个结构体用来表示事物的宽度和高度. public typealias NSSize = CGSize public struct CGSize { public var width: ...