-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrootToleave.cpp
More file actions
76 lines (61 loc) · 1.22 KB
/
rootToleave.cpp
File metadata and controls
76 lines (61 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
#include <vector>
using namespace std;
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int data)
{
struct Node* new_node=(struct Node*)malloc(sizeof(struct Node));
new_node->data=data;
new_node->left=NULL;
new_node->right=NULL;
return new_node;
}
void printThegraph(struct Node* node)
{
if(node==NULL)
return;
else
{
cout<<node->data<<" ";
if(node->left!= NULL)printThegraph(node->left);
if(node->right!= NULL)printThegraph(node->right);
}
}
void print(int arr[],int len)
{
for(int i=0;i<len;i++)
cout<<arr[i]<<" ";
cout<<"\n";
}
void printPath(struct Node* head ,int arr[], int temp)
{
if(head==NULL)
return;
arr[temp++]=head->data;
if(head->left==NULL && head->right==NULL )
print(arr,temp);
printPath(head->left,arr,temp);
printPath(head->right,arr,temp);
}
int main()
{
int temp=0;
int arr[5];
struct Node* head=createNode(12);
head->left=createNode(10);
head->right=createNode(8);
head->left->left=createNode(5);
head->left->right=createNode(9);
head->right->left=createNode(7);
head->right->right=createNode(3);
printf("This is the main");
printThegraph(head);
cout<<"\n";
printPath(head,arr,temp);
return 0;
}