## 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 {
private:
vector<vector<int> > listAll;
vector<int> list;
void ifFind(TreeNode * node , int left)
{
//存入路径list
list.push_back(node->val);

//是否是叶子节点，且路径和一致
if((left - node->val == 0) && (node->left == nullptr) && (node->right == nullptr))
{
listAll.push_back(list); //{10,5,7} ==> {10,12}
}
else
{
if(node->left)
{
ifFind(node->left, left - node->val);
}
if(node->right)
{
ifFind(node->right, left - node->val);
}
}
list.pop_back(); //{10,5,4} => ture => true
}
public:
vector<vector<int> > FindPath(TreeNode* root,int expectNumber)
{
//{10,5,4,7,12}
if(root != nullptr)
{
ifFind(root,expectNumber);
}
return listAll;
}
};
```

### Guess you like

Origin www.cnblogs.com/xiexinbei0318/p/11432876.html
Recommended
Ranking
Daily