Open In App

Difference Between Sums of Odd Level and Even Level Nodes of a Binary Tree using JavaScript

Last Updated : 27 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a Binary Tree, our task is to find the difference between the sum of nodes at odd level and the sum of nodes at even level using JavaScript.

Example:

Input:          15
/ \
12 16
/ \ \
11 14 18
/ / \
13 17 19
Output: -19
Explanation: In the above tree,
sum of nodes at odd level is (15 + 11 + 14 + 18)
which is 58. And sum of nodes at even level is
(12 + 16 + 13 + 17 + 19) which is 77.
The output for following tree should be
58 – 77 which is -19. 

These are the following approaches:

We will explore all the above methods along with their basic implementation with the help of examples.

Using Recursive Depth-First Search (DFS)

In this approach we use recursion to perform a depth-first traversal (DFS) of the binary tree. For each node, we calculate the difference between the sums of its odd-level and even-level descendant nodes. The difference for each node is given by the node's value minus the differences calculated for its left and right subtrees.

Example: In this example we are using Recursive Depth-First Search (DFS) to find the difference between sums of odd level and even level nodes of a Binary Tree

JavaScript
// Definition of a binary tree node
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Recursive function to calculate 
// the level difference
function getLevelDiff(node) {
    // Base case: if the node is null,
    // return 0
    if (node === null) {
        return 0;
    }

    // Calculate the difference for 
    // the left and right subtrees
    let leftDiff = getLevelDiff(node.left);
    let rightDiff = getLevelDiff(node.right);

    // Return the current node's value
    // minus the differences of the subtrees
    return node.data - leftDiff - rightDiff;
}

// Example usage
let root = new Node(15);
root.left = new Node(12);
root.right = new Node(16);
root.left.left = new Node(11);
root.left.right = new Node(14);
root.left.right.left = new Node(13);
root.right.right = new Node(18);
root.right.right.right = new Node(19);
root.right.right.left = new Node(17);

console.log(
    "Difference between sums of odd level and even level nodes: "
    + getLevelDiff(root));

Output
Difference between sums of odd level and even level nodes: -19

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

Auxiliary Space: O(h) where h is height of the binary tree due to recursion call.

Using Level Order Traversal

In this approach we use a queue to perform level order traversal (BFS) of the binary tree. First, we maintain two sums, one for nodes at odd levels and another for nodes at even levels. During the traversal, we keep track of the current level and add node values to the appropriate sum based on whether the level is odd or even. Finally, we calculate the difference between the sums of odd level nodes and even level nodes.

Example: In this example we are using Level Order Traversal (BFS) to find the difference between sums of odd level and even level nodes of a Binary Tree.

JavaScript
// Definition of a binary tree node
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Function to find the difference between
// sums of odd level and even level nodes
function evenOddLevelDifference(root) {
    if (root === null) {
        return 0;
    }

    // Queue for level order traversal
    let queue = [];
    queue.push(root);

    let oddSum = 0, evenSum = 0;
    let level = 0;

    while (queue.length > 0) {
        let size = queue.length;
        level++;

        // Traverse all nodes at the 
        // current level
        while (size > 0) {
            let node = queue.shift();

            // Add node value to the 
            // appropriate sum
            if (level % 2 === 0) {
                evenSum += node.data;
            } else {
                oddSum += node.data;
            }

            // Add left and right 
            // children to the queue
            if (node.left !== null) {
                queue.push(node.left);
            }
            if (node.right !== null) {
                queue.push(node.right);
            }
            size--;
        }
    }

    return oddSum - evenSum;
}

// Example usage
let root = new Node(15);
root.left = new Node(12);
root.right = new Node(16);
root.left.left = new Node(11);
root.left.right = new Node(14);
root.left.right.left = new Node(13);
root.right.right = new Node(18);
root.right.right.right = new Node(19);
root.right.right.left = new Node(17);

console.log(
    "Difference between sums of odd level and even level nodes: "
    + evenOddLevelDifference(root));

Output
Difference between sums of odd level and even level nodes: -19

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

Auxiliary Space: O(N) where N is the number of nodes in the binary tree.


Next Article
Article Tags :

Similar Reads