二叉树路径和
//一棵二叉树每个结点包含一个整数,请设计一个算法输出所有满足条件的路径:此路径上的所有结点之和等于给定值。注意此类路径不要求必须从根节点开始 void printBuffer(vector<int> buffer, int level, int i1) { for (int i=level;i<=i1;i++) { cout<<buffer[i]<<" "; } cout<<endl; } void findTarget(TreeNode *head, int target, vector<int> buffer, int level) { if(head==NULL)return; int curSum=target; buffer.push_back(head->data); for (int i=level;i>=0;i--) { curSum-=buffer[i]; if(curSum==0) printBuffer(buffer, i, level); } findTarget(head->left, target, buffer, level+1); buffer.pop_back(); level--; findTarget(head->right, target, buffer, level+1); }