VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-absolute-difference-between-the-right-view-sum-of-two-binary-trees/

⇱ Find the Absolute Difference between the Right View Sum of two Binary Trees - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the Absolute Difference between the Right View Sum of two Binary Trees

Last Updated : 23 Dec, 2023

Given two binary trees, the task is to find the sum of the absolute differences between the right view of binary trees.

Examples:

Input: Tree 1:

10
/ \
5 20
/ \ \
4 7 30

Tree 2:

50
/ \
25 60
/ \ / \
20 30 55 70

Output: 120
Explanation: For Tree 1, the right view nodes are 10, 20, and 30. Therefore, the right view sum is 10 + 20 + 30 = 60, and For Tree 2, the right view nodes are 50, 60, and 70. Therefore, the right view sum is 50 + 60 + 70 = 180. The absolute difference between the right view sum of Tree 1 and Tree 2 is |180 - 60| = 120. Therefore, the output is 120.

Input: Tree 1:

1
/ \
2 3
/ \ / \
4 5 6 7

Tree 2:

6
/ \
7 5
/ \ / \
1 5 4 3

Output: 3
Explanation: Right view sum of Tree 1: 1 + 3 + 7 = 11, Right view sum of Tree 2: 6 + 5 + 3 = 14. Thus |14 - 11| = 3

Approach: To solve the problem follow the below idea:

This problem requires us to calculate the sum of nodes that are visible from the right side of the binary tree and then find the absolute difference between the sums of the right view of two given binary trees. To calculate the sum of nodes that are visible from the right side of the binary tree, we can do a level-order traversal of the binary tree and for each level, we can add the rightmost node's value to the sum. We can use a queue to perform the level-order traversal of the binary tree. We first push the root node into the queue and then continue to process nodes until the queue becomes empty. For each level of the binary tree, we set a flag to indicate that the next node to be processed is the rightmost node. Then we pop each node from the queue and add its value to the sum if the flag is set. We then push its right and left child nodes into the queue if they exist. If we have processed all the nodes in a level, we reset the flag to indicate that the next node to be processed is the rightmost node of the next level. Once we have calculated the right view sum of both binary trees, we can find the absolute difference between them and return the result.

Steps of the approach:

  • Create a function absoluteDifference that calculates the absolute difference of the right view sums for two binary trees.
  • Check if either of the input roots is NULL and return an invalid value (e.g., -1) if so.
  • Initialize two variables, sum1 and sum2, to store the right view sums for the two trees.
  • Use two queues, q1 and q2, to perform level-order traversal of the two trees simultaneously.
  • While either of the queues is not empty, perform the following steps for each level:
  • Get the size of the current queue to process all nodes at the same level.
  • Iterate through the nodes in the first queue (q1) and update sum1 with the data of the rightmost node in the level.
  • If a node has left and right children, add them to the queue.
  • Repeat similar steps for the second queue (q2) to update sum2.
  • Calculate the absolute difference between sum1 and sum2.

Implementation of the above approach:


Output
Absolute Difference of Right View Sums: 120

Time Complexity: O(N), where N is the total number of nodes in both trees.
Auxiliary Space: O(N)

Comment