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

]

class Solution {
public:
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
        vector<vector<int> > ans;
		vector<int> temp;
		if (root!=NULL)
			pathSumUtil(root , sum , 0 , ans , temp);
		return ans;
    }
private:

	void pathSumUtil(TreeNode *root, int sum , int cur , vector<vector<int> > &ans , vector<int> &temp)
	{
		temp.push_back(root->val);
		if(root->left==NULL && root->right==NULL)
		{
			if( cur+root->val == sum )
				ans.push_back(temp);
		}
		else
		{
			if(root->left!=NULL)
				pathSumUtil( root->left, sum, cur+root->val, ans , temp );
			if(root->right!=NULL)
				pathSumUtil( root->right, sum, cur+root->val, ans , temp );
		}
	    temp.pop_back();
	}
};
时间: 2024-08-29 19:54:06

leetcode_113_Path Sum II的相关文章

hdoj 1977 Consecutive sum II

Consecutive sum II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2523    Accepted Submission(s): 1219 Problem Description Consecutive sum come again. Are you ready? Go ~~1    = 0 + 12+3+4    =

【LeetCode】167. Two Sum II - Input array is sorted

Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they a

【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_113题——Path Sum II(深度优先搜索)

Path Sum II Total Accepted: 41402 Total Submissions: 155034My Submissions Question Solution 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

【leetcode】Combination Sum II

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 t

LeetCode: Combination Sum II 解题报告

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 ta

LeetCode: Combination Sum II [039]

[题目] 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

[LeetCode][JavaScript]Combination Sum II

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 t

LeetCode 040 Combination Sum II

题目要求: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 (includ