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类似,不同的是这道题要求找到所有和等于给定值的路径,必须遍历完所有的路径。在找到满足的路径之后,不能直接返回,而是将其添加到一个vector<vector<int>>中。在查找的过程中,每经过一个结点,先使用一个vector<int>将该路径中的所有结点记录下来。需要注意的是,在进入每一个结点的时候,先将该结点的值pushvector中,在退出时将该结点的值pop出来,这样就可以避免有时会忘记pop结点的值的情况。

该方法的时间复杂度为O(n),空间复杂度为O(logn)。

三.示例代码

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> > Result;
        vector<int> TmpResult;

        pathSum(root, sum, Result, TmpResult);

        return Result;
    }

private:
    void pathSum(TreeNode* root, int sum, vector<vector<int> > &Result, vector<int> &TmpResult)
    {
        if (!root)
            return;
        if (!root->left && !root->right && root->val == sum)
        {
            TmpResult.push_back(sum);
            Result.push_back(TmpResult);

            // pop the leaf node
            TmpResult.pop_back();

            return;
        }

        int SumChild = sum - root->val;

        TmpResult.push_back(root->val);

        pathSum(root->left, SumChild, Result, TmpResult);
        pathSum(root->right, SumChild, Result, TmpResult);

        TmpResult.pop_back();
    }
};

四.小结

与Path Sum一样,用DFS解决。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 02:04:28

leetcode笔记:Path Sum 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 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 不同

[LeetCode][JavaScript]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 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] ] https://leetco

[Leetcode Week14]Path Sum II

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

LeetCode 113. Path Sum II 20170705 部分之前做了没写的题目

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][Java] 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] ] 题意: 给定一棵二叉树和一个和,找出从根节

[C++]LeetCode: 91 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 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 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[Tree]: 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, return 这个题目适合用递归来解,我的C++代码实现如下: class Solution { public: vector<vector<int> > pathSum

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 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] ] 上一道题的延伸,求出所有结果. /** * Defi