java - Printing branches on a Binary Tree -


how count number of branches, in case branches integers. here's have far. seems work couple of cases.

public int evenbranches() {     return evenbranches(overallroot); }  private int evenbranches(inttreenode root) {     if (root == null) {         return 0;     }      int val = 0;     if (root.left != null) {         val += evenbranches(root.left);     } else if (root.right != null) {         val += evenbranches(root.right);     }     if (root.data % 2 == 0) {         return val + 1;     } else {         return val;     }  } 

enter image description here

you can modify evenbranches() method below: think cover edge cases, if testcase left, let me know, fix it.

    public int evenbranches() {         return evenbranches(overallroot, 0);     }      private int evenbranches(inttreenode root, int count) {         if(root == null || (root.left == null && root.right == null)) {             return count;         }         if(root.data % 2 == 0) {             count++;         }         count += evenbranches(root.left, count);         count += evenbranches(root.right, count);         return count;     }