https://leetcode.com/problems/path-sum-ii/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
	vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
		vector<vector<int>> r;
		if (root == nullptr) {
			return r;
		}
		if (root->left != nullptr) {
			auto left = pathSumSide(root-> left, root->val, targetSum);
			if (left.size() > 0) {
				left.insert(left.begin(), root->val);
				r.push_back(left);
			}
		}
		if (root->right != nullptr) {
			auto right = pathSumSide(root->right, root->val, targetSum);
			if (right.size() > 0) {
				right.insert(right.begin(), root->val);
				r.push_back(right);
			}
		}
		return r;
	}

	vector<int> pathSumSide(TreeNode* node, int sum, int targetSum) {
		vector<int> r;
		sum += node->val;
		if (node->left == nullptr && node->right == nullptr) {
			if (sum == targetSum) {
				r.push_back(node->val);
				return r;
			}
		}
		if (node->left != nullptr) {
			auto ret = pathSumSide(node->left, sum, targetSum);
			if (ret.size() > 0) {
				ret.insert(ret.begin(), node->val);
				return ret;
			}
		}
		if (node->right != nullptr) {
			auto ret = pathSumSide(node->right, sum, targetSum);
			if (ret.size() > 0) {
				ret.insert(ret.begin(), node->val);
				return ret;
			}
		}
		return r;
	}
};

失败的错误用例:

[1]
1

增加只有一个节点的判断:

if (root->left == nullptr && root->right == nullptr) {
	if (root->val == targetSum) {
		vector<int> t;
		t.push_back(root->val);
		r.push_back(t);
	}
}

目前只考虑了两种情况,实际应该有很多种情况,参见用例

[1,0,1,1,2,0,-1,0,1,-1,0,-1,0,1,0]
2

通过 backtracking + DFS 解决

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
	vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
		if (root == nullptr) {
			return res;
		}
		backtracking(root, 0, targetSum);
		return res;
	}
private:
	vector<vector<int>> res;
	vector<int> track;

	void backtracking(TreeNode* node, int sum, int targetSum) {
		bool leaf = node->left == nullptr && node->right == nullptr;
		if (leaf) {
			if (sum + node->val == targetSum) {
				track.push_back(node->val);
				res.push_back(track);
				track.pop_back();
			}
			return;
		}
		if (node->left != nullptr) {
			track.push_back(node->val);
			backtracking(node->left, sum + node->val, targetSum);
			track.pop_back();
		}

		if (node->right != nullptr) {
			track.push_back(node->val);
			backtracking(node->right, sum + node->val, targetSum);
			track.pop_back();
		}
	}
};

测试用例:

[5,4,8,11,null,13,4,7,2,null,null,5,1]
22
[]
0
[1,0,1,1,2,0,-1,0,1,-1,0,-1,0,1,0]
2
[1,2]
1
[0,1,1]
1