-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.cpp
More file actions
171 lines (171 loc) · 3.53 KB
/
practice.cpp
File metadata and controls
171 lines (171 loc) · 3.53 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int key;
struct Node *left;
struct Node *right;
Node(int k)
{
key=k;
left=NULL;
right=NULL;
}
};
void inorder(Node *root)
{
if(root==NULL)
return;
inorder(root->left);
cout<<root->key<<" ";
inorder(root->right);
}
void preorder(Node *root)
{
if(root==NULL)
return;
cout<<root->key<<" ";
preorder(root->left);
preorder(root->right);
}
void postorder(Node *root)
{
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->key<<" ";
}
int height(Node *root)
{
if(root==NULL)
return 0;
return (1+max(height(root->left),height(root->right)));
}
void kdist(Node *root,int k)
{
if(root==NULL)
return;
if(k==0)
cout<<root->key<<" ";
else
{
kdist(root->left,k-1);
kdist(root->right,k-1);
}
}
void bfs(Node *root)
{
if(root==NULL)
return;
queue<Node *>q;
q.push(root);
while(q.size()!=0)
{
Node *curr=q.front();
q.pop();
cout<<curr->key<<" ";
if(curr->left!=NULL)
q.push(curr->left);
if(curr->right!=NULL)
q.push(curr->right);
}
}
void levelorderline(Node *root)
{
if(root==NULL)
return;
queue<Node *>q;
q.push(root);
while(q.size()!=0)
{
int cnt=q.size();
for(int i=0;i<cnt;i++)
{
Node *curr=q.front();
q.pop();
cout<<curr->key<<" ";
if(curr->left!=NULL)
q.push(curr->left);
if(curr->right!=NULL)
q.push(curr->right);
}
cout<<endl;
}
}
int size(Node *root)
{
if(root==NULL)
return 0;
else
return (1+size(root->left)+size(root->right));
}
int maxNode(Node *root)
{
if(root==NULL)
return INT_MIN;
return max(root->key,max(maxNode(root->left),maxNode(root->right)));
}
void leftView(Node *root)
{
if(root==NULL)
return;
queue<Node *>q;
q.push(root);
while(q.empty()==false)
{
int cnt=q.size();
for(int i=0;i<cnt;i++)
{
Node *curr=q.front();
q.pop();
if(i==0)
cout<<curr->key<<" "; //not using else break; as hume aur ander jana h.
if(curr->left!=NULL)
q.push(curr->left);
if(curr->right!=NULL)
q.push(curr->right);
}
}
}
//Children Sum prop-> if the sum of the children is equal to the key at node. for all nodes.
bool childrenSum(Node *root)
{
if(root==NULL)
return true;
if(root->left==NULL&&root->right==NULL)
return true;
int sum=0;
if(root->left!=NULL)
sum+=root->left->key;
if(root->right!=NULL)
sum+=root->right->key;
return(root->key==sum&&childrenSum(root->left)&&childrenSum(root->right));
}
int main()
{
Node *root=new Node(10);
root->left=new Node(20);
root->right=new Node(30);
root->left->left=new Node(40);
root->left->right=new Node(50);
root->right->left=new Node(60);
inorder(root);
cout<<endl;
preorder(root);
cout<<endl;
postorder(root);
cout<<endl;
cout<<height(root)<<endl;
kdist(root,2);
cout<<endl;
bfs(root);
cout<<endl;
levelorderline(root);
cout<<size(root)<<endl;
cout<<maxNode(root)<<endl;
if(childrenSum(root))
cout<<"haa hai bhai"<<endl;
else
cout<<"Nahi hai bhai"<<endl;
}