Path Sum I&&II
I 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 andsum = 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.
刚开始想用回溯算法,但是后来发现有负数的情况下这种方法不行,所以就不能用回溯算法了,直接用简单粗暴的递归算法。
/**
* 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 judge(TreeNode *root, int sum,int flag)
{
if(root==NULL)
return false;
if(root->left==NULL&&root->right==NULL)
return sum==root->val+flag;
return judge(root->left,sum,flag+root->val)||judge(root->right,sum,flag+root->val);
}
bool hasPathSum(TreeNode *root, int sum) {
return judge(root,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]
]
/**
* 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 {
private:
vector<vector<int>> res;
vector<int> tempres;
public:
void subSum(TreeNode* root,int tempSum,int Sum)
{
if(root==NULL)
return ;
else if((tempSum+root->val==Sum)&&(root->left==NULL&&root->right==NULL))
{
tempres.push_back(root->val);
res.push_back(tempres);
}
else
{
tempres.push_back(root->val);
subSum(root->left,tempSum+root->val,Sum);
subSum(root->right,tempSum+root->val,Sum);
}
tempres.pop_back();
return;
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if(root==NULL)
return res;
else
{
subSum(root,,sum);
return res;
}
}
};
Path Sum I&&II的更多相关文章
- [Leetcode][JAVA] Path Sum I && II
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- 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 ...
- Path Sum I && II & III
Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that ad ...
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
1. Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...
- 【leetcode】Path Sum I & II(middle)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode 112 113] - 路径和I & II (Path Sum I & II)
问题 给出一棵二叉树及一个和值,检查该树是否存在一条根到叶子的路径,该路径经过的所有节点值的和等于给出的和值. 例如, 给出以下二叉树及和值22: 5 / \ 4 8 ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
随机推荐
- POI 2018.10.22
[POI2015]ODW 喵锟讲过.分块. N>=blo,那就暴力倍增往上跳.O(N/blo*logN) N<blo,预处理,f[i][j]表示,i往上跳,每次跳j步,到根节点为止,权值和 ...
- Golang命名规范和开发规范
目录 命名 文件命名 package 变量 常量 接口 结构体 方法 注释 README 命名 文件命名 文件命名一律采用小写,不用驼峰式,尽量见名思义,看见文件名就可以知道这个文件下的大概内容. 其 ...
- rn初体验
react-native 需要的工具 .nodejs .rn cli .xcode and as ---------------- 打开终端,切换到根路径(mac中修改npm的默认安装来源) 一.op ...
- group by多字段分组
在平时的开发任务中我们经常会用到MYSQL的GROUP BY分组, 用来获取数据表中以分组字段为依据的统计数据.比如有一个学生选课表,表结构如下: Table: Subject_Selection S ...
- uva 11424
uva 11424 GCD - Extreme (I) 题意:思路:(见http://www.cnblogs.com/Duahanlang/p/3184994.html ) 差别在于数据规模和时间,其 ...
- java中String字符串的替换函数:replace与replaceAll的区别
例如有如下x的字符串 String x = "[kllkklk\\kk\\kllkk]";要将里面的“kk”替换为++,可以使用两种方法得到相同的结果 replace(CharSe ...
- Tomcat设置开启时自动访问某个servlet类存在的问题
<servlet> <servlet-name>****</servlet-name> <servlet-class>****</servlet- ...
- 51Nod 1092 回文字符串 | 最长公共子序列变形
求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...
- java 7修改文件权限
Full control over file attributes is available in Java 7, as part of the "new" New IO faci ...
- 适配器模式 C#
适配器模式 将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. ● Target目标角色:该角色定义把其他类转换为何种接口,也就是我们的期望接 ...