Difference between sums of odd level and even level nodes of a Binary Tree | Java program to find difference between sums of odd level and even level nodes of binary tree ; User defined node class ; Constructor to create a new tree node ; return difference of sums of odd level and even level ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check if level no . is even or odd and accordingly update the evenSum or oddSum ; check for left child ; check for right child ; Driver code ; construct a tree
import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = right = null ; } } class GFG { static int evenOddLevelDifference ( Node root ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int level = 0 ; int evenSum = 0 , oddSum = 0 ; while ( q . size ( ) != 0 ) { int size = q . size ( ) ; level ++ ; while ( size > 0 ) { Node temp = q . remove ( ) ; if ( level % 2 == 0 ) evenSum += temp . data ; else oddSum += temp . data ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; size -- ; } } return ( oddSum - evenSum ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 5 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 6 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 4 ) ; root . left . right . left = new Node ( 3 ) ; root . right . right = new Node ( 8 ) ; root . right . right . right = new Node ( 9 ) ; root . right . right . left = new Node ( 7 ) ; System . out . println ( " diffence ▁ between ▁ sums ▁ is ▁ " + evenOddLevelDifference ( root ) ) ; } }