要求
给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径。比如下图中,给定路径之和为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. WPF-控件-层级控件-TreeView

    <?xml version="1.0" encoding="utf-8" ?> <Data xmlns=""> &l ...

  2. python之setattr,getattr,hasattr

    可以使用setattr(), getattr(), hasattr()动态对实例进行操作. 相当于Java中的反射机制, 或者更确切地, 像JavaScript中属性操作. 具体属性: __dict_ ...

  3. Netsharp快速入门(之12) 销售管理(开发发货单工作区)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 4.4     发货单 4.4.1  部件工作区设置 1.设置部件工作区,需要设置的部件如下 2.设置单据和明细列表的字段,设置完成 ...

  4. java 获取获取字符串编码格式

    public static String getEncoding(String str) { String encode = "GB2312"; try { if (str.equ ...

  5. 【BZOJ】【2179】FFT快速傅里叶

    FFT 做的第二道用到FFT的……好吧其实还是模板题-_-b 百度上说好像分治也能做……不过像FFT这种敲模板的还是省事=.= /*********************************** ...

  6. CSS学习------之简单图片切换

    最近一直在重温纯CSS,学习的时候真的才发现,css真的博大精深啊! 所以趁着学习的劲头,谢了个最简单的CSS图片切换! 先整理下思路: 首先我希望图片居中间,两边有个切换按钮,点击按钮的时候,可以实 ...

  7. HTTP请求报文与响应报文

    http://docs.telerik.com/fiddler/KnowledgeBase/HTTP HTTP请求报文与响应报文 HTTP http://www.w3.org/Protocols/rf ...

  8. 基于jQuery的TreeGrid组件详解

    一.TreeGrid组件相关的类 1.TreeGrid(_config) _config:json格式的数据,组件所需要的数据都通过该参数提供. 2.TreeGridItem(_root, _rowI ...

  9. wordpress数据库优化wp_posts表 OPTIMIZE

    wordpress数据库优化wp_posts表 对 MySQL 数据记录进行插入.更新或删除时,会占有不同大小的空间,记录就会变成碎片,且留下空闲的空间.就像具有碎片的磁盘,会降低性能,需要整理,因此 ...

  10. zju 1037 Gridland(找规律,水题)

    题目链接 多写几个案例,根据数据的奇偶性,就能找到规律了 #include<stdio.h> int main() { int t,n,m; double ans; scanf(" ...