112. Path Sum 113. Path Sum II

回溯法的典型

1、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 a binary tree node.
* 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) {
if(root==NULL)
return false;
if(root->val==sum&&root->left==NULL&&root->right==NULL)
return true;
return (hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val));
}
};

2、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>>paths;
vector<int>path;
findPaths(root,sum,path,paths);
return paths;
}
void findPaths(TreeNode*root,int sum,vector<int>&path,vector<vector<int>>&paths)
{
if(!root)
return;
path.push_back(root->val);
if(!root->left&&!root->right&&root->val==sum)
paths.push_back(path);
findPaths(root->left,sum-root->val,path,paths);
findPaths(root->right,sum-root->val,path,paths);
path.pop_back();
}
};

时间: 2024-10-22 09:05:36

112. Path Sum 113. Path Sum II的相关文章

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium 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

LeetCode 113: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] ] Subscribe to see which co

leetcode-combination sum and combination sum II

Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includ

os模块 os.stat(&#39;path/filename&#39;) os.path.dirname(path) os.path.exists(path)&#160; os.path.join(path1[, path2[, ...]])

提供对操作系统进行调用的接口 1 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 2 os.chdir("dirname")  改变当前脚本工作目录:相当于shell下cd 3 os.curdir  返回当前目录: ('.') 4 os.pardir  获取当前目录的父目录字符串名:('..') 5 os.makedirs('dirname1/dirname2')    可生成多层递归目录 6 os.removedirs('dirname1')    若

hdu 3473 Minimum Sum(划分树-sum操作)

划分树.只是考虑求当前区间大于第k值的值得和,和小于第k值的和.显然可以在查询的时候直接搞出来.sum[d][i]表示第d层子区间l,r种l-i的和.写错了一个下标,检查了半辈子... #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue&g

sys.path和os.path

sys.path和os.path1.sys.path是python搜索模块的路径集合,是个list:os.path是os的一个模块,是操作文件和目录的模块 2.sys.path和PYTHONPATH首先PYTHONPATH可以通过sys.path来查看可以通过sys.path.append(path)将某些文件路径添加到PYTHONPATH,但是在退出python环境后自己添加的路径会消失 3.如何添加永久性PYTHONPATH将py文件放到site_packages目录下面

c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538

第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a>>b; cout<<"a+b="<<sum<<endl; return 0;} //原因sum=a+b;此语句位置不对,变量a,b在没有赋值时就被相加,超出int最大值范围.只能得到最大值65538 #include <iostream>

Cookie rejected: Illegal path attribute &quot;/nexus&quot;. Path of origin: &quot;/content/&quot; 解决方案

问题描述 通过执行"mvn clean deploy" 命令 将 Maven 项目发布到 Nexus 私服时,控制台输出了如下警告信息: [INFO] Downloaded: dav:http://maven.mysite.com/content/repositories/snapshots/${groupId}/${artifactId}/${version}/maven-metadata.xml (2 KB at 10.5 KB/sec) [INFO] Uploading: htt

path.join()与path.resolve()区别

1.path.resolve([...paths]) path.resolve() 方法会把一个路径或路径片段的序列解析为一个绝对路径. 给定的路径的序列是从右往左被处理的,后面每个 path 被依次解析,直到构造完成一个绝对路径. 例如,给定的路径片段的序列为:/foo./bar.baz,则调用 path.resolve('/foo', '/bar', 'baz') 会返回 /bar/baz. 如果处理完全部给定的 path 片段后还未生成一个绝对路径,则当前工作目录会被用上. 生成的路径是规