Offer prove safety [] [] binary tree and a path value

Title: input an integer and a root node of a binary tree, the binary print values ​​of nodes in the input path and all integers. Forming a path to the path definition begins from the root node of the tree down to the leaf node has been traversed nodes. (Note: the return value in the list, a large array Array front)


A: Before selecting order traversal, as preorder traversal to access the root node

  Recursive selected from back to front, each value obtained by subtracting the current node, until the leaf node traversal, if the value is reduced to 0 to find a path and, fill in listAll

  If the current is not a leaf node and the remaining value is not yet zero, the recursive call left subtree of the node, then recursively invokes the right child node of the tree

  If not met, the deleted node

struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
class Solution {
    vector<vector<int> > listAll;
    vector<int> list;
    void ifFind(TreeNode * node , int left)
        if((left - node->val == 0) && (node->left == nullptr) && (node->right == nullptr))
            listAll.push_back(list); //{10,5,7} ==> {10,12}
                ifFind(node->left, left - node->val);
                ifFind(node->right, left - node->val);
        list.pop_back(); //{10,5,4} => ture => true
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) 
        if(root != nullptr)
        return listAll;



Guess you like