VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-sum-level-except-triangular-number-in-binary-tree/

⇱ Maximum Sum Level Except Triangular Number in Binary Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum Sum Level Except Triangular Number in Binary Tree

Last Updated : 23 Jul, 2025

Given a Binary tree, Then the task is to output the maximum sum at any level excluding Triangular numbers. Xth Triangular numbers is the sum of starting X natural numbers, i.e. {1, 3, 6, 10..} are Triangular numbers.

Examples:

Input:

👁 T1
Input Tree

Output: 31

Explanation: The sum at each level excluding Triangular numbers is:

  • Level 1: 0 (1 is a Triangular number)
  • Level 2: 0 (3 and 6 are Triangular number)
  • Level 3: 9+22 = 31 (28 and 36 are Triangular numbers)
  • Level 4: 14 (45 is a Triangular number)

Since, 31 is the maximum sum at 3rd level of tree. Therefore, output is 31.

Input:

👁 T2
Input Tree

Output: 75

Explanation: It can be verified that the maximum sum at any level excluding Triangular numbers will be 75.

Approach 1: Implement the idea below to solve the problem

The problem can be solved using BFS algorithm. Iterate on the nodes at each level of Tree and calculate the sum excluding Triangular numbers. Update the sum at each level with maximum sum and return it after end of BFS.

  • Identifying a Triangular number: Any number X is triangular, If (8 * X + 1) is a perfect square.

Follow the steps to solve the problem:

  • Create a variable let say MaxSumLevel to store the maximum sum.
  • At iteration of each level using BFS follow the below-mentioned steps:
    • Create a variable let say Sum
    • If (Current node is not Triangular number), Then add it into Sum.
    • Push the children of current node Queue.
    • Update MaxSumLevel as max(MaxLevelSum, Sum)
  • Return MaxSumLevel.

Below is the implementation for the above approach:


Output
maximum sum of level is : 31

Time Complexity: O(N), where N is the number of nodes in the given binary tree.
Space Complexity: O(N)

Approach 2: Implement the idea below to solve the problem

The problem can be solved by performing a depth-first traversal (DFS) on the binary tree. During the traversal, calculate the sum at each level while excluding triangular numbers. Keep track of the maximum sum encountered. Return the maximum sum after the traversal completes.

  • Identifying a Triangular number: A number X is triangular if (8 * X + 1) is a perfect square.

Follow the steps to solve the problem:

1. Define a recursive function, let's say findMaxSumLevel, to perform DFS traversal on the binary tree.

2. In the findMaxSumLevel function:

  • Base case: If the current node is NULL, return.
  • Initialize the sum at the current level to 0.
  • If the current node's data is not a triangular number, add it to the sum.
  • Recursively call findMaxSumLevel for the left and right child nodes, incrementing the level by 1.

3. In the main function:

  • Create an empty vector, levelSums, to store the sum at each level.
  • Call findMaxSumLevel with the root node, passing levelSums by reference and starting the level at 0.
  • Find the maximum sum among the level sums stored in levelSums.
  • Return the maximum sum.

Below is the implementation for the above approach:


Output
maximum sum of level is : 31

Time Complexity: O(N), where N is the number of nodes in the given binary tree.

Space Complexity: O(H), where H is the height of the binary tree.

Comment