Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- void traversal(map<int, vector<int>> &mp, TreeNode* A, int level){
- if(A != NULL)
- mp[level].push_back(A->val);
- if(A->left)
- traversal(mp, A->left, level+1);
- if(A->right)
- traversal(mp, A->right, level);
- }
- vector<int> Solution::solve(TreeNode* A) {
- map<int, vector<int>> mp;
- traversal(mp, A, 0);
- vector<int>res;
- for(auto it: mp){
- for(auto it2 : it.second){
- res.push_back(it2);
- }
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement