Binary Tree = [ ] (empty Binary Tree) or: Node / \ / \ / \ / \ Bin Tree Bin Tree 
Example:


Example:

This problem is:
sum( root ) // sum all values in the binary tree starting at root 

compute sum( binary tree starting at root ): 1. leftSubTreeSum = sum( binary tree starting at root.left ); rightSubTreeSum = sum( binary tree starting at root.right ); // Solve 2 smaller problems 2. mySum = root.value + leftSubTreeSum + rightSubTreeSum; // Use the solution of // the smaller problems to // solve the original prob. 

public static double sum( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { mySum = 0; // Solution for the base case return mySum; // Return solution } else { leftSum = sum( root.left ); // Solve smaller problem 1 rightSum = sum( root.right ); // Solve smaller problem 2 mySum = root.value + leftSum + rightSum; // Solve my problem using // solution of smaller problem return mySum; // Return solution } } 
public static void main( String[] args ) { BST myBST = new BST(); // I use my old BST to create a Binary Tree myBST.put(3.0); myBST.put(6.0); myBST.put(9.0); myBST.put(1.0); myBST.put(4.0); myBST.put(8.0); myBST.put(2.0); myBST.put(7.0); System.out.println("myBST:"); myBST.printBST(); System.out.println(); double s; s = testProg.sum( myBST.root ); System.out.println("s = " + s); } 
Output:
myBST: [9.0] [8.0] [7.0] [6.0] [4.0] [3.0] [2.0] [1.0] ================================ s = 40.0 
How to run the program:


public static void print( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { return; // Done, nothing to print } else { print( root.left ); // Print the left tree print( root.right ); // Print the right tree System.out.print( root.value ); // Print the root value return; // Done } } 
Just like printing a list, we can change the order of the 3 highlighted statements in the above algorithm.

which represents the following arithmetic expression:
(3 + 4) * (1  2) 

public static void print( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { return; // Done, nothing to print } else { // System.out.print( "(" ); // Improves printing print( root.left ); // Print the left tree System.out.print( root.value ); // Print the root value // BETWEEN the left and right subtrees print( root.right ); // Print the right tree // System.out.print( ")" ); // Improves printing return; // Done } } 
Output:
Binary tree:: [2] [] [1] [*] [4] [+] [3] ================================ Output: 3 + 4 * 1  2 (No braces, you can improve it) 
You can improves the printing with braces when you uncomment the 2 additional print statements:
The output will become:
(((3 )+ (4 ))* ((1 ) (2 ))) 
How to run the program:

3 4 + means: 3 + 4 
Rule:

Example:
3 4 + 1 2  * = 3 4 + 1 2  * = (3 + 4) (1  2) * = (3 + 4 * (1  2) infix notation 

public static void print( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { return; // Done, nothing to print } else { print( root.left ); // Print the left tree print( root.right ); // Print the right tree System.out.print( root.value ); // Print the root value LAST return; // Done } } 
Output:
Binary tree:: [2] [] [1] [*] [4] [+] [3] ================================ Output: 3 4 + 1 2  * 
How to run the program:


E.g.:
+ 3 4 means: 3 + 4 
Rule:

Example:
* + 3 4  1 2 = * + 3 4  1 2 = * (3 + 4) (1  2) = (3 + 4 * (1  2) infix notation 

public static void print( Node head ) { double mySum, leftSum, rightSum; if ( head == null ) { return; // Done, nothing to print } else { System.out.print( root.value ); // Print the root value FIRST print( root.left ); // Print the left tree print( root.right ); // Print the right tree return; // Done } } 
Output:
Binary tree:: [2] [] [1] [*] [4] [+] [3] ================================ Output: * + 3 4  1 2 
How to run the program:
