05(May) Vertical sum
05. Vertical Sum
The problem can be found at the following link: Question Link
Problem Description
Given a binary tree with ( n ) nodes, find the vertical sum of the nodes that are in the same vertical line. Return all sums through different vertical lines starting from the left-most vertical line to the right-most vertical line.
Example:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7Output:
4 2 12 3 7Explanation: The tree has 5 vertical lines:
Line 1 has only one node 4 => vertical sum is 4.
Line 2 has only one node 2 => vertical sum is 2.
Line 3 has three nodes: 1, 5, 6 => vertical sum is ( 1 + 5 + 6 = 12 ).
Line 4 has only one node 3 => vertical sum is 3.
Line 5 has only one node 7 => vertical sum is 7.
My Approach
Traversal:
Traverse the binary tree in level order using a queue.
Maintain a map to store the vertical sums, where the keys represent the vertical positions and the values represent the sum of nodes at that position.
Sum Calculation:
For each node encountered during traversal, update the sum in the map corresponding to its vertical position.
Extract Sums:
After traversal, extract the sums from the map in order and store them in a vector.
Return:
Return the vector containing the vertical sums.
Time and Auxiliary Space Complexity
Expected Time Complexity: ( O(n \log n) ), as we traverse the binary tree in level order, which takes ( O(n) ) time, and building the map takes ( O(n \log n) ) time due to the insertion and sorting of keys.
Expected Auxiliary Space Complexity: ( O(n) ), as we use a queue for level order traversal and a map to store the vertical sums.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated