-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerticalOrderTraversal.cpp
More file actions
37 lines (35 loc) · 1.32 KB
/
Copy pathVerticalOrderTraversal.cpp
File metadata and controls
37 lines (35 loc) · 1.32 KB
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
vector<vector<int> > Solution::verticalOrderTraversal(TreeNode* A) {
queue < pair <TreeNode*,int> > q;
vector < vector < int > > v;
if (!A) //Don't forget
return v;
int len=0;
map < int , vector < int > > ma;
q.push({A,0});
while(!q.empty())
{
pair < TreeNode*,int > p=q.front();
q.pop();
ma[p.second].push_back(p.first->val);
if(p.first->left!=NULL)
q.push({p.first->left,p.second-1});
if(p.first->right!=NULL)
q.push({p.first->right,p.second+1});
}
for(auto &i : ma)
v.push_back(i.second);
return v;
}
//https://www.geeksforgeeks.org/print-binary-tree-vertical-order/ "(no maps)"get line numbers and print when line number occurs
//(Best)--https://www.geeksforgeeks.org/print-a-binary-tree-in-vertical-order-set-3-using-level-order-traversal/ "(order maintained)"
//https://www.geeksforgeeks.org/print-binary-tree-vertical-order-set-2/ "(with maps order not maintained)"
//https://www.interviewbit.com/problems/vertical-order-traversal-of-binary-tree/