The sum of the path II

The sum of the path II

Given a binary tree and a target and to find the sum of all paths from the root node of the leaf node is equal to a given target and path.

Description: leaf node is a node has no child nodes.

Example:
Given the following binary tree, and the target and sum = 22,

			  5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1

return:

[
   [5,4,11,2],
   [5,8,4,5]
]

Code and ideas

cpp Code 1

Submit code

/**
 * 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();
	}

};

Test code

#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 code

/**
 * 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();
    }
}

Preliminaries - preorder

/*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);
}
Published 151 original articles · won praise 47 · Views 230,000 +

Guess you like

Origin blog.csdn.net/e891377/article/details/103585701