【Path Sum II】cpp
题目:
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]
]
代码:
/**
* 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> > ret;
// Terminal conditon 1 : to the null (not leaf node)
// even if sum equals zero at the time, becasue not leaf node
// so it is also not the available solution path
if (!root) return ret;
// Terminal condition 2 : leaf node
if ( !root->left && !root->right )
{
// if leaf node's val equal sum
if ( sum==root->val )
{
vector<int> tmp;
tmp.insert(tmp.begin(),root->val);
ret.push_back(tmp);
}
return ret;
}
// not leaf node : move forward to left and right
vector<vector<int> > l = Solution::pathSum(root->left, sum - root->val);
vector<vector<int> > r = Solution::pathSum(root->right, sum - root->val);
for ( size_t i = ; i<l.size(); ++i )
{
l[i].insert(l[i].begin(), root->val);
ret.push_back(l[i]);
}
for ( size_t i = ; i<r.size(); ++i )
{
r[i].insert(r[i].begin(), root->val);
ret.push_back(r[i]);
}
return ret;
}
};
tips:
与Path Sum思路类似(http://www.cnblogs.com/xbf9xbf/p/4508964.html)
不同的地方是:只要不是leaf node,left和right两边的情况都要考虑。
============================================
并且有个地方需要缕清思路:如果递归时遇上root==NULL,直接返回空的ret是否合理?如果此时的sum==0呢?
root==NULL有以下三种情况
1. 如果整棵树是空树:即使sum==0也不满足条件
2. 某个非leaf node的left(或right)为空:则即使此时sum==0,往left(或right)方向走会得到root=NULL,因为此时root不是leaf node也不成立
============================================
第二次过这道题,DFS的思路比较清晰。头几次漏掉了onePath.push_back(root->val)这个语句,补上以后AC了。
/**
* 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> > ret;
vector<int> onePath;
if ( root ) Solution::psum(root, sum, ret, onePath);
return ret;
}
static void psum(TreeNode* root, int sum, vector<vector<int> >& ret, vector<int> onePath)
{
if ( !root->left && !root->right )
{
if ( root->val==sum )
{
onePath.push_back(root->val);
ret.push_back(onePath);
return;
}
}
onePath.push_back(root->val);
if ( root->left ) Solution::psum(root->left, sum-root->val, ret, onePath);
if ( root->right ) Solution::psum(root->right, sum-root->val, ret, onePath);
}
};
【Path Sum II】cpp的更多相关文章
- 【二叉树的递归】04找出二叉树中路径和等于给定值的所有路径【Path Sum II】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...
- 【Combination Sum II 】cpp
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【Jump Game II 】cpp
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 【Single Num II】cpp
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
随机推荐
- Linux ed命令
$ ed <- 激活 ed 命令 a <- 告诉 ed 我要编辑新文件 My name is Titan. <- 输入第 ...
- linux下怎么编译运行C语言程序?
linux下的C语言编译器是gcc,C++的编译器是g++. linux下编程可以使用编辑器vi或vim,建议使用vim,因为它有语法高亮显示.程序编写好后,假设你的程序名为test.c,可以使用gc ...
- Entity Framework with NOLOCK
在SqlServer中,频繁在同一个数据库表同时进行读写的时候,会存在锁的问题,也就是在前一个insert.update.delete事务操作完毕之前,你不能进行读取,必须要等到操作完毕,你才能进行s ...
- varnish状态引擎1
vcl: state engine:各引擎之间存一定程度上的相关性:前一个engine如果可以有多种下游engine,则上游engine需要用return指明 要转移的下游engine vcl_rec ...
- 基于.NET的WebService的实现和WCF的实现
1.新建一个MVC web项目. 2.点击项目,[右键]→[添加]→[新建项] 3.点击[Web]→[Web服务] 4.恭喜,Web Service已经新建成功,里面的方法就可以参考着根据自己的需要进 ...
- MySQL性能优化笔记整理
一.测试篇 1.测试目的,就是量化找出短板(基础参数配置) 2.测试三大指标 IOPS:每秒处理的IO请求数,即IO响应速度(注意和IO吞吐量的区别) QPS:每秒请求(查询)次数 TPS:每秒事务数 ...
- Oracle之Linux下核心参数
kernel.shmmax 用于定义单个共享内存段的最大值: 建议一个大的共享内存段能容纳整个SGA,这样在任何时候都不会有性能下降的隐患: 建议:32位Linux 物理内存大于4G 的设置为4G 即 ...
- 微信支付开发h5发起支付再次签名,返回给h5前端
注意:参数区分大小写.
- iOS关于打包出错
运行没问题,有可能是自动打包编译脚本的存在,删除掉即可.
- 【坑】执行Consumer的时候发生java.net.UnknownHostException错误
[时间]: 2016/4/8 17:30 [问题]: kafka执行Consumer实例的时候,发生了一下错误. kafka配置文件server.properties如下: zookeeper配置文件 ...