Write a program to calculate the difference between the sum of the odd level and even level nodes of a Binary Tree

import java.util.Queue;

public class DiffOddEven {

//Represent a node of binary tree
public static class Node{
int data;
Node left;
Node right;

public Node(int data){
//Assign data to the new node, set left and right children to null
this.data = data;
this.left = null;
this.right = null;
}
}

//Represent the root of binary tree
public Node root;

public DiffOddEven(){
root = null;
}

//difference() will calculate the difference between sum of odd and even levels of binary tree
public int difference() {
int oddLevel = 0, evenLevel = 0, diffOddEven = 0;

//Variable nodesInLevel keep tracks of number of nodes in each level
int nodesInLevel = 0;

//Variable currentLevel keep track of level in binary tree
int currentLevel = 0;

//Queue will be used to keep track of nodes of tree level-wise

//Check if root is null
if(root == null) {
System.out.println("Tree is empty");
return 0;
}
else {
//Add root node to queue as it represents the first level
currentLevel++;

while(queue.size() != 0) {

//Variable nodesInLevel will hold the size of queue i.e. number of elements in queue
nodesInLevel = queue.size();

while(nodesInLevel > 0) {
Node current = queue.remove();

//Checks if currentLevel is even or not.
if(currentLevel % 2 == 0)
//If level is even, add nodes's to variable evenLevel
evenLevel += current.data;
else
//If level is odd, add nodes's to variable oddLevel
oddLevel += current.data;

if(current.left != null)
if(current.right != null)
nodesInLevel--;
}
currentLevel++;
}
//Calculates difference between oddLevel and evenLevel
diffOddEven = Math.abs(oddLevel - evenLevel);
}
return diffOddEven;
}

public static void main (String[] args) {

DiffOddEven bt = new DiffOddEven();
//Add nodes to the binary tree
bt.root = new Node(1);
bt.root.left = new Node(2);
bt.root.right = new Node(3);
bt.root.left.left = new Node(4);
bt.root.right.left = new Node(5);
bt.root.right.right = new Node(6);

//Display the difference between sum of odd level and even level nodes
System.out.println("Difference between sum of odd level and even level nodes: " + bt.difference());
}
}

Output:

Difference between sum of odd level and even level nodes: 11
Author's Other questions
 Write a program in Java to find the maximum and minimum value node from a circular linked list. Write a program in Java to create a doubly linked list containing n nodes How to perform quicksort in Java? How to perform merge sort in Java? How to perform Linear Search in Java? How to perform Selection Sort in Java? How to perform Binary Search in Java? How to perform Bubble Sort in Java? Can RMI and CORBA based applications interact? What is JRMP? What is the use of HTTP-tunneling in RMI? What are the steps involved to write RMI based programs? What is the purpose of stub and skeleton? What do you understand by the bean persistent property? What is the purpose of using the Java bean? What is a JavaBean? How will you load a specific locale? What is Locale? Can you write a Java class that could be used both as an applet as well as an application? What is an applet?