// BTDepth - calculate and print the depth of the binary tree public class BTDepth { public static void main(String[] args) { BTNode root; root = beginningTree(); root.preorderPrint(); System.out.println(); root.inorderPrint(); System.out.println(); root.postorderPrint(); root.print(0); int max = maxDepth(root, 0); System.out.println("Tree depth = " + max); } public static BTNode beginningTree() { BTNode root; BTNode child; root = new BTNode(14, null, null); child = new BTNode(17, null, null); root.setLeft(child); child.setLeft(new BTNode(9, null, null)); child.setRight(new BTNode(53, null, null)); child = child.getLeft(); child.setLeft(new BTNode(13, null, null)); child = child.getLeft(); child.setRight(new BTNode(21, null, null)); child = new BTNode(11, null, null); root.setRight(child); child.setLeft(new BTNode(4, null, null)); child = child.getLeft(); child.setLeft(new BTNode(19, null, null)); child.setRight(new BTNode(7, null, null)); return root; } public static int maxDepth(BTNode t, int depth) { int lMax; int rMax; if (t != null) { if (t.isLeaf()) return depth; else { lMax = maxDepth(t.getLeft(), depth+1); rMax = maxDepth(t.getRight(), depth + 1); if (lMax > rMax) return lMax; else return rMax; } } else return 0; } }