LeetCode之“树”:Path Sum && Path Sum II
Path Sum
题目要求:
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
这道题采用深度优先搜索的方法就可以了,具体程序(12ms)如下:
/**
* Definition for a binary tree node.
* 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)
return false;
return hasPathSumSub(root, , sum);
} bool hasPathSumSub(TreeNode *tree, int subSum, int sum)
{
if(!tree)
return false; subSum += tree->val;
if(!tree->left && !tree->right && subSum == sum)
return true; return hasPathSumSub(tree->left, subSum, sum) || hasPathSumSub(tree->right, subSum, sum);
}
};
Path Sum II
题目要求:
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
这题与上题类似。具体程序(24ms)如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> rVec;
if(!root)
return rVec; vector<int> vec;
hasPathSumSub(root, vec, rVec, , sum);
return rVec;
} void hasPathSumSub(TreeNode *tree, vector<int> vec, vector<vector<int>>& rVec, int subSum, int sum)
{
if(!tree)
return; vec.push_back(tree->val);
subSum += tree->val;
if(!tree->left && !tree->right && subSum == sum)
rVec.push_back(vec); hasPathSumSub(tree->left, vec, rVec, subSum, sum);
hasPathSumSub(tree->right, vec, rVec, subSum, sum);
}
};
LeetCode之“树”:Path Sum && Path Sum II的更多相关文章
- Leetcode题 112 和 113. Path Sum I and II
112题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- 32. Path Sum && Path Sum II
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...
- 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...
- Path Sum,Path Sum II
Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 【Leetcode】【Easy】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
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...
随机推荐
- Scala:集合类型Collection和迭代器
http://blog.csdn.net/pipisorry/article/details/52902549 Scala Collection Scala 集合分为可变的和不可变的集合. 可变集合可 ...
- [系统运维]Supervisord安装和启动程序
supervisord 是client/server 系统 把不是守护进程的进程变成守护进程 监控它自己启动的进程,类似于看门狗 可以作为开机启动的一种封装 可以精确控制进程的状态,而不是pidfil ...
- [Android]聊聊ActionMode
最近一段时间都没有更新文章,趁工作之余,更新一篇. 今天介绍一个很常见效果也最容易被忽略的弹出框:ActionMode.主要是ActionMode使用和自己使用过程中遇到的一些问题,相对还是比较简单的 ...
- android的消息通知栏
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- Java基本语法-----java注释
1注释的作用 通过注释提高程序的可读性,是java程序的条理更加清晰,易于区分代码行与注释行.另外通常在程序开头加入作者,时间,版本,要实现的功能等内容注释,方便后来的维护以及程序员的交流. 2注释的 ...
- Eclipse编写ExtJS卡死问题 eclise js验证取消
1. Eclipse编写ExtJS卡死问题 eclise js验证取消 近期项目用到了extjs,发现项目编译的时候特别的卡,浪费很多时间而且保存的时候还要编译,因此打算看下如何取消验证extjs.最 ...
- Android简易实战教程--第十二话《代码获取手机总运行内存的大小》
手机RAM存储,类似于电脑的内存.这一篇,对通过代码获取手机总内存大小做详细介绍. 首先,定义一个engine类,这个类功能就是获取进程信息,包括运行的程序个数,系统总内存,系统剩余总内存.本篇先完成 ...
- EBS中的采购单据状态及其控制
李 颖 (济南钢铁股份有限公司 装备部,山东 济南 250101) 摘 要:介绍了Oracle Purchasing模块中采购单据的管理与控制,结合实例,分析了各状态下可采取的控制活动及控制活 ...
- Servlet3.0注解@WebInitParam和@WebServlet
在以前的servlet中我们初始化一些参数都是配置在web.xml中的,自从servlet3.0之后给我们提供了注解@WebServlet和@WebInitParam,@WebServlet是用来配置 ...
- android api 镜像站
项目地址:https://github.com/msdx/androiddoc 访问短址: http://androiddoc.qiniudn.com/