路径总和 II
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
给定如下二叉树,以及目标和 sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
返回:
[
[5,4,11,2],
[5,8,4,5]
]
代码与思路
cpp代码1
提交代码
/**
* Definition for a binary tree node.
* 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> path;//当前的路径
int path_value = 0;//当前的路径和
preorder(root, path_value, sum, path, result);
return result;
}
private:
void preorder(TreeNode *node, int &path_value, int sum,
vector<int> &path,
vector<vector<int> > &result){
if (!node){
return;
}
path_value += node->val;
path.push_back(node->val);
if (!node->left && !node->right && path_value == sum){
result.push_back(path);
}
preorder(node->left, path_value, sum, path, result);
preorder(node->right, path_value, sum, path, result);
path_value -= node->val;
path.pop_back();
}
};
测试代码
#include <stdio.h>
#include <vector>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
std::vector<std::vector<int> > pathSum(TreeNode* root, int sum) {
std::vector<std::vector<int> > result;//记录所有满足条件的路径
std::vector<int> path;//当前的路径
int path_value = 0;//当前的路径和
preorder(root, path_value, sum, path, result);
return result;
}
private:
void preorder(TreeNode *node, int &path_value, int sum,
std::vector<int> &path,
std::vector<std::vector<int> > &result){
if (!node){
return;
}
path_value += node->val;
path.push_back(node->val);
if (!node->left && !node->right && path_value == sum){
result.push_back(path);
}
preorder(node->left, path_value, sum, path, result);
preorder(node->right, path_value, sum, path, result);
path_value -= node->val;
path.pop_back();
}
};
int main(){
TreeNode a(5);
TreeNode b(4);
TreeNode c(8);
TreeNode d(11);
TreeNode e(13);
TreeNode f(4);
TreeNode g(7);
TreeNode h(2);
TreeNode x(5);
TreeNode y(1);
a.left = &b;
a.right = &c;
b.left = &d;
c.left = &e;
c.right = &f;
d.left = &g;
d.right = &h;
f.left = &x;
f.right = &y;
Solution solve;
std::vector<std::vector<int> > result = solve.pathSum(&a, 22);
for (int i = 0; i < result.size(); i++){
for (int j = 0; j < result[i].size(); j++){
printf("[%d]", result[i][j]);
}
printf("\n");
}
return 0;
}
java代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<List<Integer>> result = new LinkedList();
Stack<Integer> path = new Stack();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
dfs(root, sum);
return result;
}
public void dfs(TreeNode root, int sum){
if(root == null) return;
if(root.left == null && root.right == null){
path.push(root.val);
if(root.val == sum){
result.add(new ArrayList(path));
}
path.pop();
return;
}
path.push(root.val);
if(root.left != null) dfs(root.left, sum - root.val);
if(root.right != null) dfs(root.right, sum - root.val);
path.pop();
}
}
预备知识–先序遍历
/*class BiTree {
int value;
BiTree lchild;
BiTree rchild;
public BiTree() {}
public BiTree(int value) {
super();
this.value = value;
}
}*/
/**
* 非递归
* @param b
*/
public static void preScan(BiTree b) {
int length = 0;
BiTree[] stack = new BiTree[20];
stack[length ++] = b;
BiTree temp;
while(length > 0) {
temp = stack[-- length];
System.out.print(temp.value + " ");
if(temp.rchild != null) {
stack[length ++] = temp.rchild;
}
if(temp.lchild != null) {
stack[length ++] = temp.lchild;
}
}
}
/**
* 递归
* @param b
*/
public static void scan(BiTree b) {
if(b != null) {
System.out.print(b.value + " ");
}
if(b.lchild != null) scan(b.lchild);
if(b.rchild != null) scan(b.rchild);
}