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>将该路径中的所有结点记录下来。由于vector<vector<int>>是用来记录所有满足的路径的,所以传递引用给它是为了对它的每一个改动都是对其本身的操作,而不是对其副本的操作,使用来返回找到的所有路径。使用的vector<int>只是拷贝操作,所以都是对其副本的操作,不会对原始的vector<int>有影响。

 

C++代码实现:

#include<iostream>
#include<new>
#include<vector>
using namespace std;

//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)
    {
        vector<vector<int> > path;
        vector<int> tmp;
        hasPathSum(root,sum,path,tmp);
        return path;
    }
    void hasPathSum(TreeNode *root, int sum,vector<vector<int> > &path,vector<int> tmp)
    {
        if(root==NULL)
            return;
        tmp.push_back(root->val);
        if(root->left==NULL&&root->right==NULL&&(sum-root->val)==0)
        {
            path.push_back(tmp);
        }
        if(root->left)
            hasPathSum(root->left,sum-root->val,path,tmp);
        if(root->right)
            hasPathSum(root->right,sum-root->val,path,tmp);
    }
    void createTree(TreeNode *&root)
    {
        int i;
        cin>>i;
        if(i!=0)
        {
            root=new TreeNode(i);
            if(root==NULL)
                return;
            createTree(root->left);
            createTree(root->right);
        }
    }
};
int main()
{
    Solution s;
    TreeNode *root;
    s.createTree(root);
    vector<vector<int> > path=s.pathSum(root,6);
    for(auto a:path)
    {
        for(auto v:a)
            cout<<v<<" ";
        cout<<endl;
    }
}

运行结果:

 

时间: 2024-08-01 06:49:24

Path Sum II的相关文章

[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] ] [分析] 题目要求求出所有路径

[LeetCode]40.Combination Sum II

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

UVa 10656 Maximum Sum (II) (water ver.)

10656 - Maximum Sum (II) Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1597 In a given a sequence of non-negative integers you will have to find such a

[LeetCode]*124.Binary Tree Maximum Path Sum

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3 Return 6. [分析]    需要考虑以上两种情况: 1 左子树或者右子树中存有最大路径和 不能和根节点形成一个路径 2 左子树 右子树 和根节点形成最大路径 [代码] /****

[LeetCode]64.Minimum Path Sum

[题目] Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. [思路] 设sum[i][j] 到位置(i,j)时路径最

[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-digits integers. For each integer in this list: The hundreds digit represents the depth D of this node, 1 <= D <= 4. The tens digit represents the positio

[LeetCode]--112. 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 tru

【LeetCode从零单排】No112 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 t

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