Q:

Creating recursive methods given BinaryTree

I'm studying for a test in a few hours, and I'm going over some practice problems but I'm really having trouble with recursion related problems. I was wondering if someone could walk me through how to do these? 
Using the BinaryTree class below, create the following recursive methods: 

sum() – returns the sum of all values in the BinaryTree. 
countGreaterThan(int) – returns the number of nodes that have values greater than the specified integer. 
isValidSearchTree() – returns true if the Binary Tree is a valid search tree and false otherwise. 

I get the basic premise of how to create these functions like for example, for the sum function it'd usually go something like:
int sum = 0
for(int i; i<binaryTree.size(); i++){
    sum += binaryTree[i]
}

Or something similar. But I don't really know how to apply these concepts to a BinaryTree with nodes and make them recursive as well? The code I'm provided with is given below.
public class BinaryTree {
 private int data;
 private BinaryTree leftChild;
 private BinaryTree rightChild;

 public BinaryTree(int val) {
     data = val;
     leftChild = null;
     rightChild = null;
  }

  // Get methods
 public int getData() { return data; }
 public BinaryTree getLeftChild() { return leftChild; }
 public BinaryTree getRightChild() { return rightChild; }

 // Set methods
 public void setData(int val) { data = val; }
 public void setLeftChild(BinaryTree left) { leftChild = left; }
 public void setRightChild(BinaryTree right) { rightChild = right; }
}

A:

First of all I think you have too many questions, but with this meta discussion in mind, I'll walk through the recursive sum function and explain how to tackle a problem like these recursively.
These algorithms all appear to require depth first search so I recommend you also check out the linked video.
The neat thing about recursion is allows us to think only about what each particular node has to do. For the sum example, that means returning the value it has stored, plus the recursive sum of its children. 
public int exampleSum(){
   // Start with the data we have.
   int sum = this.getData();

   // Then get the data we need (from each child, which can be done recursively).
   if (this.getLeftChild() != null) {
       sum += this.getLeftChild().exampleSum();
   }
   // Do the same for the right node.

   return sum; 
}

What this equates to is going through each node, and getting the sum of each subtree. 
Imagine its a group of people standing in the shape of a binary tree, where everyone has a 'parent' and 0 to 2 'children', and they all hold a number. 
Instead of someone going through and asking each person what their number is and adding them all up (looping), we start at the top, the first person asks their children for their sum, and their children ask their children, etc (recursion). 
Each person adds the number their children gave them to their number, then hands it to their parent.
The two special cases are when the person has no children, so they stop and just hand their number to their parent, and the 'root' node who gives their number to whatever function asked them in the first place.
I recommend drawing a binary search tree and going through this algorithm yourself.
I hope this was helpful, and if it was could you like this answer. 
In future I recommend asking one question at a time. Good luck with your test!

