fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int data;
  5. node* left, *right;
  6. };
  7. node* newNode(int data){
  8. node* root = new node();
  9. root->data = data;
  10. root->left = root->right = NULL;
  11. return root;
  12. }
  13. vector<int> v;
  14. void printPath(node* root){
  15. if(root==NULL)return;
  16. v.push_back(root->data);
  17.  
  18. if(root->left==NULL && root->right==NULL){
  19. for(int i=0; i<v.size(); i++)cout<<v[i]<<" ";
  20. cout<<endl;
  21. printPath(root->left);
  22. v.pop_back();
  23. printPath(root->right);
  24. v.pop_back();
  25.  
  26. }
  27. }
  28. int main(void){
  29. node* root = newNode(1);
  30. root->left = newNode(2);
  31. root->right = newNode(3);
  32. root->left->left = newNode(4);
  33. root->left->right = newNode(5);
  34. root->left->right->left = newNode(6);
  35. root->left->right->right = newNode(7);
  36. printPath(root);
  37. return 0;
  38. }
Success #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
Standard output is empty