Skip to content

Commit

Permalink
Vertical Order Traversal of a Binary Tree
Browse files Browse the repository at this point in the history
Solution to Vertical Order Traversal of a Binary Tree -> issue Google-Developer-Student-Club-CCOEW#98
By Isha Baviskar
GitHub ID -> isha0904
  • Loading branch information
isha0904 authored Oct 21, 2023
1 parent 0cf4909 commit e029437
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions VerticalOrderTraversalOfBT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

//Solution to Issue #98 -> Vertical Order Traversal of Binary Tree

class Solution {
public:
vector<vector<int>> verticalTraversal(TreeNode* root) {
// Answer vector to store the vertical traversal results.
vector<vector<int>> ans;

// Queue to perform a level-order traversal of the binary tree.
queue<pair<TreeNode*, pair<int, int>>> q;

// Traverse from the root node
q.push({root, {0, 0}});

// Map to store nodes based on their column, row, and values.
map<int, map<int, multiset<int>>> mp;

// Level-order traversal of the binary tree.
while (!q.empty()) {
// Get the front element of the queue.
auto front = q.front();
q.pop();

TreeNode* &node = front.first;
auto &coordinate = front.second;
int &row = coordinate.first;
int &col = coordinate.second;

// Current node
mp[col][row].insert(node->val);

// for Left child
if (node->left)
q.push({node->left, {row + 1, col - 1}});

// for right child
if (node->right)
q.push({node->right, {row + 1, col + 1}});
}

// Map to construct the final result vector.
for (auto it : mp) {
auto &colmap = it.second;
vector<int> vline;

for (auto mpit : colmap) {
auto &mset = mpit.second;
vline.insert(vline.end(), mset.begin(), mset.end());
}

// Adding the values from the current column to the answer vector.
ans.push_back(vline);
}

// Return the vertical traversal result.
return ans;
}
};


/*
By Isha Baviskar ([email protected])
ID -> isha0904
*/

0 comments on commit e029437

Please sign in to comment.