Advertisement
Jathin

Untitled

Sep 29th, 2021
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. /**
  2.  * Definition for binary tree
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8.  * };
  9.  */
  10.  
  11. void traversal(map<int, vector<int>> &mp, TreeNode* A, int level){
  12.     if(A != NULL)
  13.         mp[level].push_back(A->val);
  14.     if(A->left)
  15.         traversal(mp, A->left, level+1);
  16.     if(A->right)
  17.         traversal(mp, A->right, level);
  18. }
  19.  
  20. vector<int> Solution::solve(TreeNode* A) {
  21.     map<int, vector<int>> mp;
  22.     traversal(mp, A, 0);
  23.     vector<int>res;
  24.     for(auto it: mp){
  25.         for(auto it2 : it.second){
  26.             res.push_back(it2);
  27.         }
  28.     }
  29.     return res;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement