-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPractice06.cpp
59 lines (45 loc) · 888 Bytes
/
Practice06.cpp
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
// we have to write an algorothm which will give max value at each level of binary treee
#include<iostream>
using namespace std;
Class Node{
int data;
Node* left;
Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
void helper(vector<int>&res, Node *root,int d){
if(d == res.size()){
res.push_back(root->data);
}
res[d] = max(res[d],root->data);
helper(res,root->left,d+1);
helper(res,root->right,d+1);
}
// 2nd method
void helper2(vector<int>res,Node *root){
queue<Node *>q;
q.push(root);
while(!q.empty()){
int n = q.size();
int maxi = INT_MIN;
for(int i = 0;i<n;i++){
Node *temp = q.front();
q.pop();
maxi =max(maxi,temp->data);
if(temp->left)
q.push(temp->left);
if(temp->right)
q.push(temp->right);
}
res.push_back(maxi);
}
return res;
}
int main(){
vector<int>res;
helper(res,root,0);
return res;
}