LeetCode:Path Sum I II
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.
递归求解,代码如下:
/**
* 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 hasPathSum(TreeNode *root, int sum) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(root == NULL)return false;
if(root->left == NULL && root->right == NULL && root->val == sum)
return true;
if(root->left && hasPathSum(root->left, sum - root->val))
return true;
if(root->right && hasPathSum(root->right, sum - root->val))
return true;
return false;
}
};
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 binary tree
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<int> > res;
if(root == NULL)return res;
vector<int> curres;
curres.push_back(root->val);
pathSumRecur(root, sum, curres, res);
return res;
}
void pathSumRecur(TreeNode *root, int sum, vector<int> &curres, vector<vector<int> >&res)
{
if(root->left == NULL && root->right == NULL && root->val == sum)
{
res.push_back(curres);
return;
}
if(root->left)
{
curres.push_back(root->left->val);
pathSumRecur(root->left, sum - root->val, curres, res);
curres.pop_back();
}
if(root->right)
{
curres.push_back(root->right->val);
pathSumRecur(root->right, sum - root->val, curres, res);
curres.pop_back();
}
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440044.html
LeetCode:Path Sum I II的更多相关文章
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [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 ...
- [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 II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 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 II路径和
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [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] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
随机推荐
- java集合 之 Map集合
Map用于保存具有映射关系的数据,具有两组值:一组用于保存Map中的key:另一组用于保存Map中的value,形成key-value的存储形式. Map集合中包含的一些方法: void clear( ...
- DP大作战——多重背包
题目描述 在之前的上机中,零崎已经出过了01背包和完全背包,也介绍了使用-1初始化容量限定背包必须装满这种小技巧,接下来的背包问题相对有些难度,可以说是01背包和完全背包的进阶问题. 多重背包:物品可 ...
- python 把数据 json格式输出
有个要求需要在python的标准输出时候显示json格式数据,如果缩进显示查看数据效果会很好,这里使用json的包会有很多操作 import json date = {u'versions': [{u ...
- JavaScript Patterns 4.4 Self-Defining Functions
If you create a new function and assign it to the same variable that already holds another function, ...
- HttpClent4.3 的例子
package com.unbank.robotspider.util; import java.io.IOException; import java.net.MalformedURLExcepti ...
- find命令中参数perm的用法
按照文件权限模式用-perm选项,按文件权限模式来查找文件的话.最好使用八进制的权限表示法.如在当前目录下查找文件权限位为755的文件,即文件属主可以读.写.执行,其他用户可以读.执行的文件,可以用: ...
- jQuery事件绑定on、off 和one,取代bind, live, delegate
jQuery最新版建议:最好用on来代替以前的bind, live, delegate,其中live是最不建议使用的. on和off的格式 on $(elements).on(events[, sel ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- 20150912华为机考1之"输入一个字符串,将其中出现次数最多的字符输出"
不吐槽华为的服务器了,直接上正文 输入:字符串(英文字母),长度不超过128 输出:出现频率最高的字母 思路写在注释文档 /* Input a string * Output the most fre ...
- hadoop core-site.xml
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text ...