fork download
  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. class node{
  8. public:
  9. int data ;
  10. node*left;
  11. node*right;
  12.  
  13. node( int d){
  14. data = d;
  15. left = NULL;
  16. right = NULL;
  17. }
  18. };
  19.  
  20. node*temp = NULL;
  21. node*buildtree(){
  22. string s;cin>>s;
  23. if(s=="false"){
  24. return NULL;
  25. }
  26. if(s=="true"){
  27. buildtree();
  28. }
  29. if(s!= "true"&&s!= "false"){
  30. node*root = new node(stoi(s));
  31. root->left = buildtree();
  32. root->right = buildtree();
  33. temp = root;
  34. }
  35. return temp;
  36. }
  37.  
  38. vector<vector<int>>ans;
  39. vector<int>res;
  40.  
  41. void rootToLeaf(node* root, int sum, int cs){
  42. if(root == NULL)return;
  43.  
  44. res.push_back(root->data);
  45. cs += root->data;
  46.  
  47. if(root->left == NULL and root->right == NULL and cs == sum){
  48. ans.push_back(res);
  49. res.pop_back();
  50. return;
  51. }
  52.  
  53. rootToLeaf(root->left,sum,cs);
  54. rootToLeaf(root->right,sum,cs);
  55. res.pop_back();
  56. }
  57.  
  58.  
  59. int main() {
  60. node*root = buildtree();
  61. int k;
  62. cin >>k;
  63. int curSum = 0;
  64.  
  65. rootToLeaf(root,k,curSum);
  66.  
  67. for(auto v : ans){
  68. for(auto x : v)cout<<x<<" ";
  69. cout<<endl;
  70. }
  71. return 0;
  72. }
Success #stdin #stdout 0.01s 5440KB
stdin
10 true 20 true 30 false false true 50 false false true 40 true 60 false false true 73 false false
60
stdout
10 20 30