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 ...
随机推荐
- 【队列】【P2827】【NOIP2016D2T3】蚯蚓
传送门 Description 本题中,我们将用符号 $\lfloor c \rfloor$ 表示对 $c$ 向下取整,例如:$\lfloor 3.0 \rfloor = \lfloor 3.1 \r ...
- VC对话框实现添加滚动条实现滚动效果
对话框滚动条及滚动效果实现,用的api主要有: ScrollWindow, SetScrollInfo, GetScrollInfo, SetWindowOrgEx.涉及的数据结构为SCROLLINF ...
- [ldap]ldap相关问题
背景: ldap数据库要同步,按照如下操作步骤: 1.导出: 使用slapcat,slapcat直接对数据库操作, slapcat 2.将所需的条目取出,生成文件in.ldif 3.在目标机器上导入: ...
- ioctrl 获取本机IP及MAC地址
通过使用ioctl可以获得本机的一些信息,这里记录获得interface IP及MAC的过程. 1:ioctl 函数的作用是什么 man ioctl: DESCRIPTION The ioctl() ...
- Leetcode 94. 二叉树的中序遍历
1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...
- ZooKeeper翻译(一)
欢迎来到Apache ZooKeeper的世界 Apache Zookeeper是一个为了开发和维护一个开源的服务的一个尝试,这个服务使高可用的分布式协作成为可能. ZooKeeper是什么? Zoo ...
- mysql数据库cmd直接登录
找到mysql的安装路径: 将该路径配置到环境变量中: win+R代开dos窗口:输入mysql -uroot -p回车,输入密码.
- asp.net 权限管理系统
asp.net webform ,基于组织机构.角色的权限管理系统. 网上找的,挺好.随拿来分享. https://bitbucket.org/zzhi/asp.net
- mave的依赖范围
compile(编译范围) compile是默认的范围:如果没有提供一个范围,那该依赖的范围就是编译范 围.编译范围依赖在所有的classpath中可用,同时它们也会被打包. provided(已提供 ...
- [NOIP2011]刷水
前几天做了NOIP2011的题,感觉不是那么难. 这边先做了两天的前两题,T3还没打. D1T1:顺次读入,分别判断是否覆盖即可,照例大水: #include<cstdio> ],b[], ...