Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Node{
- public:
- int data;
- Node *left,*right;
- Node(int key){
- data=key;
- left=NULL;
- right=NULL;
- }
- };
- void preorder(Node *root){
- if(root==NULL)
- return;
- cout<<" "<<root->data;
- preorder(root->left);
- preorder(root->right);
- }
- void PrintArray(int arr[],int len){
- cout<<endl;
- for(int i=0;i<len;i++)
- cout<<" "<<arr[i];
- }
- void PrintPathRecur(Node *root,int arr[],int path_len){
- if(root==0)
- return;
- arr[path_len]=root->data;
- path_len++;
- if(root->left==NULL && root->right ==NULL)
- PrintArray(arr,path_len);
- else {
- PrintPathRecur(root->left,arr,path_len);
- PrintPathRecur(root->right,arr,path_len);}
- }
- int PrintPaths(Node *root){
- int path[1000];
- PrintPathRecur(root,path,0);
- }
- int main(){
- Node *root=NULL;
- Node *newnode=new Node(1);
- root=newnode;
- root->left=new Node(2);
- root->right=new Node(3);
- (root->left)->left=new Node(4);
- (root->left)->right=new Node(5);
- cout<<"pre order traversal ";
- preorder(root);
- PrintPaths(root);
- return 0;
- }
Add Comment
Please, Sign In to add comment