Norway


// structure to store a Tree node

struct Node

{

    int data;

    Node *left, *right;

};

 

// Helper function

void findDiff(Node *root, int &diff, int level)

{

    // base case

    if (root == nullptr)

        return;

    // if current level is

    if (level & 1)

        diff += root->data;

    // if current level is even

    else

        diff -= root->data;

    // recurse for left subtree and right subtree

    findDiff(root->left, diff, level + 1);

    findDiff(root->right, diff, level + 1);

}

// Function to calculate the between of all

// at odd and sum of all nodes present at even level

int findDiff(Node *root)

{

    int diff = 0;

    findDiff(root, diff, 1);

    return diff;

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here