Difference between sums of odd level and even level nodes of a Binary Tree | A recursive C # program to find difference between sum of nodes at odd level and sum at even level ; A binary tree node ; The main function that return difference between odd and even level nodes ; Base case ; Difference for root is root 's   data - difference for left subtree  - difference for right subtree  ; Driver program to test above functions
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right ; } } public class BinaryTree { public Node root ; public virtual int getLevelDiff ( Node node ) { if ( node == null ) { return 0 ; } return node . data - getLevelDiff ( node . left ) - getLevelDiff ( node . right ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . left . right . left = new Node ( 3 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . right = new Node ( 9 ) ; tree . root . right . right . left = new Node ( 7 ) ; Console . WriteLine ( tree . getLevelDiff ( tree . root ) + " ▁ is ▁ the ▁ required ▁ difference " ) ; } }