Advertisement
Kali_prasad

sum of root to path numbers

May 9th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef pair<string,int> psi;
  10. typedef unordered_map<int,int> mii;
  11. typedef unordered_map<long long,long long> mll;
  12. typedef unordered_map<string,int> msi;
  13. typedef unordered_map<char,int> mci;
  14. typedef unordered_set<int> si;
  15. typedef unordered_set<long long> sll;
  16. typedef unordered_set<string> ss;
  17. typedef unordered_set<char> sc;
  18. typedef map<int,int> ormii;
  19. typedef map<long long,long long> ormll;
  20. typedef map<string,int> ormsi;
  21. typedef map<char,int> ormci;
  22. typedef set<int> orsi;
  23. typedef set<long long> orsll;
  24. typedef set<string> orss;
  25. typedef set<char> orsc;
  26. typedef vector<int> vi;
  27. typedef vector<string> vs;
  28. typedef vector<char> vc;
  29. typedef vector<ll> vll;
  30. typedef vector<vector<int>> vvi;
  31. typedef vector<vector<string>> vvs;
  32. typedef vector<vector<ll>> vvll;
  33.  
  34. #define FOR(i, a, b) for (auto i=a; i<=(b); i++)
  35. #define FORd(i,b,a) for (int i =b; i >= a; i--)
  36. #define sortinc(v)  sort(v.begin(),v.end())
  37. #define sortdec(v)  sort(v.rbegin(),v.rend())
  38. #define sz(x) (int)(x).size()
  39. #define mp make_pair
  40. #define pb push_back
  41. #define pob pop_back
  42. #define pf push_front
  43. #define pof pop_front
  44. #define fi first
  45. #define se second
  46. #define ins insert
  47.  
  48. const int MOD = 1000000007;
  49. //type functions here
  50.  
  51. class Node
  52. {
  53.     public:
  54.   Node* left;
  55.   Node* right;
  56.     int data;
  57.     Node(int data)
  58.     {
  59.        this->data=data;
  60.         left=NULL;
  61.         right=NULL;
  62.     }
  63. };
  64. int sum;
  65. Node* insert(Node* root,int data)
  66. {
  67.     if(!root) return new Node(data);
  68.     if(data<root->data)
  69.         root->left=insert(root->left,data);
  70.     else
  71.         root->right=insert(root->right,data);
  72.     return root;        
  73. }
  74.  
  75. int digits(int d)
  76. {
  77.     int c=0;
  78.     while(d)
  79.     {
  80.         d/=10;
  81.         c++;
  82.     }
  83.     return c;
  84. }
  85. void print(Node* root,int temp)
  86. {
  87.     if(root==NULL) return;
  88.     int sizee=digits(root->data);
  89.        
  90.     while(sizee){
  91.     temp=(temp*10)%MOD;
  92.     sizee--;
  93.     }
  94.    
  95.     temp=(temp+root->data)%MOD;
  96.     if(!root->left&&!root->right)
  97.     {
  98.         sum=(sum+temp)%MOD;
  99.     }
  100.     if(root->left)print(root->left,temp);
  101.     if(root->right)print(root->right,temp);
  102.    
  103.    
  104. }
  105.  
  106. int main() {
  107.     ios_base::sync_with_stdio(false);
  108.     cin.tie(NULL);
  109.    
  110.     int tc=1;
  111.     cin>>tc;
  112.     FOR(w,1,tc)
  113.     {
  114.        int n;
  115.         cin>>n;
  116.         Node* root=NULL;
  117.         FOR(ww,1,n)
  118.         {
  119.             int temp;
  120.             cin>>temp;
  121.             root=insert(root,temp);
  122.         }
  123.         if(root==NULL) continue;
  124.         sum=0;
  125.         print(root,0);
  126.         cout<<sum<<endl;
  127.        
  128.     }
  129.     return 0;
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement