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 ...
随机推荐
- maven的setting.xml文件中只配置本地仓库路径的方法
maven的setting.xml文件中只配置本地仓库路径的方法 即:settings标签下只有一个 localRepository标签,其他全部注释掉即可 <?xml version=&quo ...
- caffe数据集——LMDB
LMDB介紹 Caffe使用LMDB來存放訓練/測試用的數據集,以及使用網絡提取出的feature(為了方便,以下還是統稱數據集).數據集的結構很簡單,就是大量的矩陣/向量數據平鋪開來.數據之間沒有什 ...
- 在某OC字符串中,搜索指定的某字符串:-rangeOfString:
NSString *originalStr = @"搜索:王者拜仁!"; NSString *subStr = @"搜索:"; // 在originalStr这 ...
- HDU4513:吉哥系列故事——完美队形II(Manacher)
吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- [洛谷P2704] [NOI2001]炮兵阵地
洛谷题目链接:[NOI2001]炮兵阵地 题目描述 司令部的将军们打算在NM的网格地图上部署他们的炮兵部队.一个NM的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示), ...
- C11性能之道:右值引用
1.左值与右值 C++11中新增了一种类型,右值引用,标记为T &&. 首先来介绍什么是左值和右值,左值是指表达式结束后依旧存在的持久对象,而右值是指表达式结束之后就不再存在的临时对象 ...
- 【点分治练习题·不虚就是要AK】点分治
不虚就是要AK(czyak.c/.cpp/.pas) 2s 128M by zhb czy很火.因为又有人说他虚了.为了证明他不虚,他决定要在这次比赛AK. 现在他正在和别人玩一个游戏:在一棵树上 ...
- 【BZOJ】1914: [Usaco2010 OPen]Triangle Counting 数三角形
[题意]给定坐标系上n个点,求能构成的包含原点的三角形个数,n<=10^5. [算法]极角排序 [题解]补集思想,三角形个数为C(n,3)-不含原点三角形. 将所有点极角排序. 对于一个点和原点 ...
- Codeforces Round #494 (Div. 3)
刚好在考完当天有一场div3,就开了个小号打了,打的途中被辅导员喊去帮忙,搞了二十分钟-_-||,最后就出了四题,题解如下:题目链接:http://codeforces.com/contest/100 ...
- Intersecting Lines (计算几何基础+判断两直线的位置关系)
题目链接:http://poj.org/problem?id=1269 题面: Description We all know that a pair of distinct points on a ...