Distribute candies in a Binary Tree

Given a binary tree with N nodes, in which each node value represents number of candies present at that node, and there are N candies in total. In one move, one may choose two adjacent nodes and move one candy from one node to another (the move may be from parent to child, or from child to parent.)

The task is to find the number of moves required such that every node have exactly one candy.

Examples:

Input :      3
           /   \
          0     0 
Output : 2
Explanation: From the root of the tree, we move one 
candy to its left child, and one candy to
its right child.

Input :      0
           /   \
          3     0  
Output :3
Explanation : From the left child of the root, we move 
two candies to the root [taking two moves]. Then, we move 
one candy from the root of the tree to the right child.

Approach:
The idea is to traverse the tree from leaf to root and consecutively balance all of the nodes. To balance a node, the number of candy at that node must be 1.

There can be two cases:

  1. If a node needs candies, if the node of the tree has 0 candies (an excess of -1 from what it needs), then we should push a candy from its parent onto the node.
  2. If the node has more than 1 candy. If it has say, 4 candies (an excess of 3), then we should push 3 candies off the node to its parent.

So, the total number of moves from that leaf to or from its parent is excess = abs(num_candies – 1).

Once a node is balanced, we never have to consider this node again in the rest of our calculation.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to distribute candies
// in a Binary Tree
  
#include <bits/stdc++.h>
using namespace std;
  
// Binary Tree Node
struct Node {
    int key;
    struct Node *left, *right;
};
  
// Utility function to create a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return (temp);
}
  
// Utility function to find the number of
// moves to distribute all of the candies
int distributeCandyUtil(Node* root, int& ans)
{
    // Base Case
    if (root == NULL)
        return 0;
  
    // Traverse left subtree
    int l = distributeCandyUtil(root->left, ans);
  
    // Traverse right subtree
    int r = distributeCandyUtil(root->right, ans);
  
    // Update number of moves
    ans += abs(l) + abs(r);
  
    // Return number of moves to balance
    // current node
    return root->key + l + r - 1;
}
  
// Function to find the number of moves to
// distribute all of the candies
int distributeCandy(Node* root)
{
    int ans = 0;
  
    distributeCandyUtil(root, ans);
  
    return ans;
}
  
// Driver program
int main()
{
    /*  3
       / \
      0   0
              
    Let us create Binary Tree 
    shown in above example */
  
    Node* root = newNode(3);
    root->left = newNode(0);
    root->right = newNode(0);
  
    cout << distributeCandy(root);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to distribute candies 
// in a Binary Tree 
class GfG 
  
// Binary Tree Node 
static class Node
{
    int key; 
    Node left, right; 
}
static int ans = 0;
  
// Utility function to create a new node 
static Node newNode(int key) 
    Node temp = new Node(); 
    temp.key = key; 
    temp.left = null;
    temp.right = null
    return (temp); 
  
// Utility function to find the number of 
// moves to distribute all of the candies 
static int distributeCandyUtil(Node root) 
    // Base Case 
    if (root == null
        return 0
  
    // Traverse left subtree 
    int l = distributeCandyUtil(root.left); 
  
    // Traverse right subtree 
    int r = distributeCandyUtil(root.right); 
  
    // Update number of moves 
    ans += Math.abs(l) + Math.abs(r); 
  
    // Return number of moves to balance 
    // current node 
    return root.key + l + r - 1
  
// Function to find the number of moves to 
// distribute all of the candies 
static int distributeCandy(Node root) 
    distributeCandyUtil(root); 
    return ans; 
  
// Driver program 
public static void main(String[] args) 
    /* 3 
    / \ 
    0 0 
              
    Let us create Binary Tree 
    shown in above example */
  
    Node root = newNode(3); 
    root.left = newNode(0); 
    root.right = newNode(0); 
  
    System.out.println(distributeCandy(root)); 
}
  
// This code is contributed by Prerna Saini.

chevron_right



Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : prerna saini