[leetcode] Path sum路径之和
要求
给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径。比如下图中,给定路径之和为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路径之和的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
随机推荐
- SQLIO Disk Subsystem Benchmark Tool
C:\Program Files (x86)\SQLIO>sqlio -? sqlio v1.5.SG -?: invalid option Usage: sqlio [options] [&l ...
- MySQL主从同步报Client requested master to start replication from position
数据库版本:5.6.16 测试环境MySQL 主从,数据库被人重启,忘记开启start slave,导致主从失效,停了一天的数据没有追上. 查看从库的数据库状态:show slave stat ...
- Android修改system只读权限
在Root后的真机上使用adb remount命令不知道为什么不行. 但有些时候还是想向system文件夹下面写文件,例如把tcpdump工具放到/system/bin 下面,然后就可以直接使用tcp ...
- z470 装黑苹果 10.92
1.分两个区,一个是mac安装区,一个是镜像拷贝区. 2.把镜像压进去. 3.安装好系统. 4.把镜像区的 extent拷贝到安装好的系统盘里去. 5.安装驱动,网盘里有.还有系统也在网盘里. 6.声 ...
- 简单的C#线程开发实例(隔一秒改变一下Label的Text)
要实现的效果:点击按纽,窗口上的label上出现1~100数字的变化. 第一个实例(把窗口上的label上文字改成0): using System; using System.Windows.Form ...
- DBA应该知道的一些SQL Server跟踪标记
跟踪标记是什么? 对于DBA来说,掌握Trace Flag是一个成为SQL Server高手的必要条件之一,在大多数情况下,Trace Flag只是一个剑走偏锋的奇招,不必要,但在很多情况下,会使用这 ...
- 10.30Daily Scrum
出席人员 任务分配完成情况 明天任务分配 王皓南 研究代码,讨论实现方法及实现的动能 研究代码,学习相应语言,讨论设计思路 申开亮 研究代码,讨论实现方法及实现的动能 研究代码,学习相应语言,讨论设计 ...
- STM32的SPI问题。
问题描述: 之前一直使用的单片机是LPC2109,对其SPI很熟悉.基本就是原本拿来稍作修改就用.由于某种原因需要使用STM32,然后设备的驱动是之前写好的,只修改了一些硬件控制端口,由于硬件驱动使用 ...
- bzoj 4010: [HNOI2015]菜肴制作 拓扑排序
题目链接: 题目 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory Limit: 512 MB 问题描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴 ...
- asp.net 通过ajax方式调用webmethod方法使用自定义类传参及获取返回参数
实体类 public class User { public int Id { get; set; } public string Name { get; se ...