二叉树路径和等于某个值

struct BtreeNode

{

int val;

BtreeNode*pleft;

BtreeNode*pright;

}

void findpath(BtreeNode*root,int target)

{

if(!root)

{

return;

}

vector<int>path;

int cursum=0;

void findpath(root,path,cursum,target);

}

findpath(BtreeNode*root,vector<int>path,int cursum,int target)

{

cursum+=root->val;

path.push_back(root->val);

if(cursum==target&&root->left=root->right==NULL)

{

cout<<"find path"<<endl;

for(int i=0;i<path.size();i++)

{

cout<<path[i]<<endl;

}

}

if(!root->left)

{

findpath(root->left,path,cursum,target);

}

if(!root->right)

{

findpath(root->right,path,cursum,target);

}

path.pop_back();

cursum-=root->val;

};

 

posted @ 2016-08-26 17:19  maxandhchen  阅读(380)  评论(0编辑  收藏  举报