二叉树-dfs确定路径个数

来源:互联网 发布:mysql greatest 编辑:程序博客网 时间:2024/06/16 12:24
判空递归=》find(),parent(),leftchild(),fan()
二叉树
判空递归=》find(),parent(),leftchild(),fan()
判空递归特殊版=》size(),height()
不判空递归=》三序,delete()
都是dfs的应用

求二叉树节点和为某数的路径个数。
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
 vector<vector<int>> v0;
     vector<int> v;
  void find(TreeNode* root,  int sum)
    {
        if (root == NULL)
{
return;
}
        v.push_back(root->val);
        if (root->left==NULL&&root->right==NULL && sum == root->val)
{
            v0.push_back(v);
}
        else
        {
            if (root->left)
                find(root->left, sum - root->val);
            if (root->right)
                find(root->right, sum - root->val);
        }
        v.pop_back();
    }
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {


        if(root)
{
find(root,expectNumber);
}
        return v0;
    

}
};





原创粉丝点击