Leetcode_Path Sum II

题意需要记录所有可能的路径,可以设置一个变量path用于存储每次产生的路径,设置一个变量result存储所有符合条件的路劲。

用递归可以实现,主要注意的问题是:对path路径的pop操作。

方式一:每一次深度遍历之后,得到一条路径,符合条件就压入result中,然后要判断下一天路径。下一条路径是将前一条路径中的最后一个左节点弹出,压入右节点。下面这种方式,就是在寻得叶子节点之后,继续执行叶子节点的左节点和右节点的遍历,两种都是return,之后执行的pop操作,就可以弹出最后压入的叶子节点。

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

方式二:

单独处理叶子节点,遇到叶子节点则不再往下遍历,将其pop出,继续下一次右节点的遍历

class Solution {
public:
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
		vector<vector<int> > result;
		vector<int> path;
		if(!root) return result;
		getPath(result,path,root,sum);
        return result;
    }
private:
	void getPath(vector<vector<int> > &result,vector<int> &path,TreeNode *root,int sum)
	{
		if(!root)
			return ;
		path.push_back(root->val);
		if(root->left ==nullptr&&root->right==nullptr&&root->val==sum)
		{
			result.push_back(path);
			path.pop_back();
			return;
		}
		if(root->left ==nullptr&&root->right==nullptr&&root->val!=sum)
		{
			path.pop_back();
			return;
		}
		getPath(result,path,root->left,sum-root->val);
		//path.pop_back();
		getPath(result,path,root->right,sum-root->val);
		path.pop_back();
	}
};
时间: 2024-10-10 11:38:51

Leetcode_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