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] More articles about

  1. 【LeetCode】Path Sum ---------LeetCode java Summary

    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

    subject : 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

    subject : 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

    subject : 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): At the node i Because of the maximum end of the unilateral path b(i): At the node i Path and a(i) = max{ i->val, i->val + max{a(i->left), a(i->righ ...

  9. Leetcode note 112 - Path Sum

    Topic link :Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

Random recommendation

  1. What wechat apps want is traffic that cannot be monitored dark social

    “ Wechat applet ” What kind of change will it bring ? It's just a super Web app? Or is it just to save space for your mobile phone ? What Tencent wants is up to 70% The above “ Huge traffic that can't be monitored ”, be called “dark social”( Dark social ). ...

  2. Redux course 2: link React

    Through the previous tutorial , We have a simple environment , And it can run Redux The program , Also on the How to write Redux Example I have a preliminary impression : Mastered Use Redux Control state transition , Then drive React Components change , This is learning R ...

  3. js Custom events

    The nature of custom events , Create an object , Then take the name of the event as an attribute of the object , then value It's a [], All callbacks of this event are push go in . Write a very basic , There's no exposure js Custom events for . var eve ...

  4. mac Some common sense of the following

    1, environment variable EddydeMacBook-Pro:~ eddy$ vi ~/.bash_profile EddydeMacBook-Pro:~ eddy$ vim /etc/profile Eddyd ...

  5. OpenStack (1) - Keystone OpenStack Identity Service

    echo deb http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main >> /etc ...

  6. C# Realize in CAD Insert another... Into the drawing DWG Block code

    C# Realize in CAD Insert another... Into the drawing DWG Block code PromptPointResult ppr = ed.GetPoint(" Please select the insertion point :"); Point3d pt = ppr. ...

  7. PHP Make a simple calendar

    Share a PHP Made calendars <?php // perpetual calendar if($_GET['year']){$year = $_GET['year'];}else{$year = date("Y&quo ...

  8. Take out js The way all the properties of an object

    Example : // Take out all the properties of the event $('#id_btn').bind("click dbclick mouseout",{crdx:' I am the value of passing '},function(e){ var ...

  9. 1.1.6- Study Opencv And MFC Hybrid programming --- Play WAV Music and alpha Fusion function

    Source code :http://download.csdn.net/detail/nuptboyzhb/3961698 Alpha Merge menu items 1.      increase alpha Merge menu items , Modify the corresponding properties , Create class wizard ...

  10. day19 Other modules

    collections modular The detailed content http://www.cnblogs.com/Eva-J/articles/7291842.html 1.namedtuple: Generate... That can use names to access element content ...