Path Sum II 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/path-sum-ii/description/


Description

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

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]
]

Solution

class Solution {
void dfs(vector<vector<int>>& res, vector<int>& path, TreeNode* root, int sum) {
if (root -> left == NULL && root -> right == NULL) {
if (sum == root -> val) {
path.push_back(root -> val);
res.push_back(path);
path.pop_back();
}
return;
}
if (root -> left != NULL) {
path.push_back(root -> val);
dfs(res, path, root -> left, sum - (root -> val));
path.pop_back();
}
if (root -> right != NULL) {
path.push_back(root -> val);
dfs(res, path, root -> right, sum - (root -> val));
path.pop_back();
}
}
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
if (root == NULL)
return res;
vector<int> path;
dfs(res, path, root, sum);
return res;
}
};

解题描述

这道题目题意是,在一棵二叉树中,寻找一个从根节点到叶子节点的路径,使得路径上的数字之和为给定的数字sum,要求找出所有这样的路径。当然最容易想到的就是使用DFS来解决。

[Leetcode Week14]Path Sum II的更多相关文章

  1. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  2. 【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 ...

  3. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  4. [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 su ...

  5. 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 su ...

  6. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

  7. LeetCode 113. Path Sum II路径总和 II (C++)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  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 su ...

  9. leetcode 113 Path Sum II ----- java

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

随机推荐

  1. Java接口成员变量

    定义接口    使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,当中接口体由常量定义和方法定义两部分组成.定义接口的基本格式例如以下: [修饰符] inter ...

  2. Activiti5工作流笔记一

    介绍工作流 网上工作流的定义一大堆,这里就不去复制了,通俗的理解,工作流就是类似OA系统中请假审批.报销审批等一系列流程,下级提交的申请只有直系领导才能审批,其他人是没有权限的,而只有直系领导审批通过 ...

  3. 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树

    题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...

  4. CentOS 文本搜索grep

    grep 用于在文本中执行关键词搜索, 用法: grep [选项]... PATTERN [FILE]... [root@bigdata-senior01 ~]# grep "ftp&quo ...

  5. 什么是Docker?(6-12)

    关于什么是Docker,刚开始学的时候一脸懵X,这个东西到底是干嘛用的啊?偶然间在知乎上刷到一个比较通俗的解释: Docker就相当于一个容器,这个容器了不得了,它里面能搭好你项目需要的所有环境,并且 ...

  6. [SP8372-TSUM]Triple Sums

    题面在这里 description 某\(B\)姓\(OJ\)权限题 给出\(n\)个正整数\(a[i]\),求\(i<j<k\)且\(S=a[i]+a[j]+a[k]\)的三元组\((i ...

  7. [CF785E]Anton and Permutation

    题目大意:有一串数为$1\sim n(n\leqslant2\times10^5)$,$m(m\leqslant5\times10^4)$次询问,每次问交换位置为$l,r$的两个数后数列中逆序对的个数 ...

  8. [SDOI2015]约数个数和 莫比乌斯反演

    ---题面--- 题解: 为什么SDOI这么喜欢莫比乌斯反演,,, 首先有一个结论$$d(ij) = \sum_{x|i}\sum_{y|j}[gcd(x, y) == 1]$$为什么呢?首先,可以看 ...

  9. 重拾C#教程:变量

    一个变量只不过是一个供程序操作的存储区的名字.在 C# 中,每个变量都有一个特定的类型,类型决定了变量的内存大小和布局.范围内的值可以存储在内存中,可以对变量进行一系列操作. 我们已经讨论了各种数据类 ...

  10. 洛谷4245:【模板】任意模数NTT——题解

    https://www.luogu.org/problemnew/show/P4245 给两个多项式,求其乘积,每个系数对p取模. 参考: 代码与部分理解参考https://www.luogu.org ...