要求
给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径。比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false.
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 5
思路
递归,从跟到叶子判断,如果在叶子处剩下的给定值恰好为给定值,那么返回ture.
参考代码
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if (root == NULL)
return false;
else if (root != NULL && root->left == NULL && root->right == NULL)
{
if (sum == root->val)
return true;
else
return false;
}
else
return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val);
}
};

扩展

求出所有符合条件的路径。例如,上题中返回<<5, 4, 11, 2>, <5, 8, 4, 5>>

参考代码

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void addPath(TreeNode *root, int sum, vector<int> tmp, vector<vector<int> > &rev)
{
if (root != NULL && root->left == NULL && root->right == NULL && root->val == sum)
{
tmp.push_back(root->val);
rev.push_back(tmp);
tmp.pop_back();
}
if (root->left != NULL)
{
tmp.push_back(root->val);
addPath(root->left, sum - root->val, tmp, rev);
tmp.pop_back();
}
if (root->right != NULL)
{
tmp.push_back(root->val);
addPath(root->right, sum - root->val, tmp, rev);
tmp.pop_back();
}
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > rev;
if (root == NULL)
return rev; vector<int> tmp; addPath(root, sum, tmp, rev);
return rev;
}
};

[leetcode] Path sum路径之和的更多相关文章

  1. [Leetcode] Path Sum路径和

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

  2. LeetCode:Path Sum I II

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

  3. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  4. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  5. [LeetCode] Path Sum 二叉树的路径和

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

  6. [LeetCode] Path Sum IV 二叉树的路径和之四

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  7. [LeetCode] 112. Path Sum 路径和

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

  8. [Leetcode] Path Sum II路径和

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

随机推荐

  1. Linux下C程序插入执行shell脚本

    1.system(执行shell命令) 相关函数 fork,execve,waitpid,popen表头文件 #include<stdlib.h>定义函数 int system(const ...

  2. 动态模板中 SWIPER 划不动问题

    原文: 地址:http://hao.jser.com/archive/8030/ 作者:segmentfault 问题: 动态循环生成swiper-slide类,在swiper-wrapper里生成6 ...

  3. C# Windows - 菜单栏和工具栏

    除了MenuStrip控件之外,还有许多控件可用于填充菜单.3个常见的控件是ToolStripMenuItem,ToolStripDropDown,和ToolStripSeparator.这些控件表示 ...

  4. How to create jar for Android Library Project

    http://stackoverflow.com/questions/17063826/how-to-create-jar-for-android-library-project This works ...

  5. OC self super isa指针

    self指针: self是oc面向对象设计中的一个特殊指针,相当于java中的this,但是比this强大,this只能访问实例对象的相关方法和成员变量,或者说this只代表实例对象: self不仅可 ...

  6. 高德开发 android 出现 key 鉴权失败

    环境windows + android studio 原因: 曾经更改过key.store 解决办法: 首先运行cmd移动到keystore的目录下keytool -list -keystore 文件 ...

  7. Ajax ContentType 列表

    ".*"="application/octet-stream" ".001"="application/x-001" & ...

  8. Windows Phone 8.1 与 Windows Phone 8.1 Silverlight区别

    以下讨论基于当前的WP8 APP, 一.Windows Phone 8 app:旧的WP8程序:不需要迁移: 二.Windows Phone 8.1 app新的使用Windows Runtime 的程 ...

  9. nenu contest3 The 5th Zhejiang Provincial Collegiate Programming Contest

    ZOJ Problem Set - 2965 Accurately Say "CocaCola"!  http://acm.zju.edu.cn/onlinejudge/showP ...

  10. 关于JS及应用程序开发的一些体会

    代码通常从 一,生命周期 二,业务流程 这几方面来看. JS Client可以和Server端分离. JS端的生命周期. Server端就是 JS能处理的只是HTTP协议.