Skip to content

Commit

Permalink
Merge pull request #204 from isha0904/VerticalOrderTraversalOfBT
Browse files Browse the repository at this point in the history
Vertical order traversal of bt
  • Loading branch information
Gdsc-Cummins authored Oct 21, 2023
2 parents 0f551cb + e029437 commit 89b6794
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
23 changes: 23 additions & 0 deletions SolutionToStickLengthsProblem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;

int main()
{
long long a, c = 0;
cin >> a;
int b[a];

for (int i; i < a; i++){
cin >> b[i];
}

sort(b, b + a);

for(int i; i < a; i++){
c += abs(b[a/2] - b[i]);
}

cout << c;

return 0;
}
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 89b6794

Please sign in to comment.