-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.cpp
More file actions
55 lines (48 loc) · 1.07 KB
/
Copy pathbinary_tree.cpp
File metadata and controls
55 lines (48 loc) · 1.07 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
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* left;
node* right;
node(int x){
data=x;
left=NULL;
right=NULL;
}
};
void print(node* root){
if(root->left==NULL&&root->right==NULL){
cout<<root->data<<"->\n";
return;
}
else if(root->left==NULL){
cout<<root->data<<"-> ,"<<root->right->data<<"\n";
print(root->right);
}
else if(root->right==NULL){
cout<<root->data<<"->"<<root->left->data<<",\n";
print(root->left);
}
else {
cout<<root->data<<"->"<<root->left->data<<","<<root->right->data<<"\n";
print(root->left);
print(root->right);
}
}
int main(){
node* a=new node(2);
node* root = a;
// a->data=2;
node* b=new node(3);
// b->data=3;
node* c=new node(4);
// c->data=4;
a->left=b;
a->right=c;
b->right=new node(5);
c->left=new node(6);
c->right=new node(7);
// cout<<a->data<<endl;
print(root);
}