Leetcode:Path Sum 二叉树路径和

Path Sum:

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.

解题分析:

二叉树首先应该想到递归

对于root = 5, 寻找sum=22的路径 这个问题可以分为  root=4 寻找sum=22-5=17的路径 和 root=8
寻找sum=22-5=17的路径 这两部分

也就是说,我们可以递归寻找左右子树路径和,我们只需要更改和值即可

终止条件:如果root为空,立即返回false, 如果root为叶子节点,如果其值为最后剩余的和值,那么就存在总的路径和值等于sum


class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if (root == nullptr) return false;
if (root->left == NULL && root->right == NULL) return sum == root->val;
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
};

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

解题分析:

现在需要求出满足条件的路径,并且是所有路径,所以左子树求到了满意结果,不能return,要接着求右子树

我们在上一题中,最后叶子结点如果满足 val == sum 那么这条路径就符合要求,此时我们将结果添加到result中

但是如果保存该路径上的所有结点值呢? 我们是 每遇到一个非空结点都添加到一个vector中(其实这个vector就是一个栈)

如果栈顶(vector最末元素)的左右子树都不存在满足条件的路径,那么就出栈,也就是 从其父节点开始继续判断

这就是类似于 二叉树的先序遍历,每遇到一个结点就入栈,随后如果不满足条件就出栈达到其父节点

先序遍历对应 根左右,也就是说最后的结果就是 从根节点出发的路径,即路径方向也吻合,而不需要逆向


class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int>> result;
vector<int> tmp;
BintreePathSum(root, sum, tmp, result);
return result;
}

void BintreePathSum(TreeNode* root, int gap, vector<int>& tmp, vector<vector<int>>& result) {
if (root == NULL) return;
tmp.push_back(root->val);

if (root->left == NULL && root->right == NULL) {
if (root->val == gap) {
result.push_back(tmp);
}
}
BintreePathSum(root->left, gap - root->val, tmp, result);
BintreePathSum(root->right, gap - root->val, tmp, result);

tmp.pop_back();
}
};

Leetcode:Path Sum 二叉树路径和,布布扣,bubuko.com

时间: 2024-10-08 09:23:51

Leetcode:Path Sum 二叉树路径和的相关文章

[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 sum. For example:Given the below binary tree andsum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题意:给定一数,在树中找出所有路径和等于该数的情况.方

LeetCode (12) Path Sum (二叉树路径和判断)

题目描述 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, return true, as there exist a root-to-lea

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 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] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

LeetCode: Path Sum II [113]

[题目] 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] ] [题意] 判断二叉树中是否存在一条从根到

[leetcode]Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/path-sum/ 题意: 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 =

LeetCode: Path Sum [112]

1. 拿到一个新 bug, 首先要重现问题. 这对 code 问题是必须的, 对客户的 data 问题, 几乎也是必须的. 如果是 code 问题, 不重现就没办法修改代码, 改好了也无法验证是不是改对了. 客户的 data 出问题, 多数情况也是能够重现的. 毕竟客户是用我们的系统操作的, 只要拿到客户的历史数据, 对照着是可以自己做出同样的数据. 以前我遇到 data fix 的时候不喜欢重现, 都是凭感觉给出脚本. 但这样常常忽略一些重要的数据, 容易出错. 如果确定是 data fix,

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 given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 Return: [ [5,4,11

[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 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] ] 分析: 这道题目算是Path Sum的延伸,只不过

LeetCode: Path Sum 解题报告

Path Sum 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             / \