Maximum spiral sum in Binary Tree
Given a binary tree containing n nodes. The problem is to find the maximum sum obtained when the tree is spirally traversed. In spiral traversal one by one all levels are being traversed with the root level traversed from right to left, then next level from left to right, then further next level from right to left and so on.
Maximum spiral sum = 4 + (-1) + (-2) + 1 + 5 = 7
Approach: Obtain the level order traversal in spiral form of the given binary tree with the help of two stacks and store it in an array. Find the maximum sum sub-array of the array so obtained.
Maximum Spiral Sum = 7
Time Complexity: O(n).
Auxiliary Space: O(n).
My Personal Notes arrow_drop_up
Please Login to comment...