Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int v[100]={0};
- struct Node
- {
- int data;
- struct Node* left, *right;
- };
- Node* newNode(int data)
- {
- Node *temp = new Node;
- temp->data = data;
- temp->left = temp->right = NULL;
- return temp;
- }
- Node* push(Node* head,int z)
- {
- Node*temp=newNode(z);
- if(head==NULL){head=temp; return head;}
- queue<Node*>q;
- Node*x=head;
- q.push(head);
- while(!q.empty())
- {
- x=q.front();
- if(x->left)q.push(x->left);
- else {x->left=temp; return head;}
- if(x->right)q.push(x->right);
- else {x->right=temp; return head;}
- q.pop();
- }
- return head;
- }
- void travel(Node *root1,int hd)
- {
- if(root1)
- {
- v[hd]+=root1->data;
- travel(root1->left,hd+1);
- travel(root1->right,hd+1);
- }
- }
- void print(Node *root)
- {
- if(root)
- {
- travel(root,0);
- sort(v,v+100);
- cout<<v[99];
- }
- }
- int main() {
- struct Node* root = NULL;
- int i,n;
- cin>>n;
- int a[n];
- for(int i=0;i<n;i++)cin>>a[i];
- root=push(root,a[i]);
- for(int i=1;i<n;i++)push(root,a[i]);
- print(root);
- return 0;
- }
Add Comment
Please, Sign In to add comment