public class MyTreeExamples { public static void main(String[] args) { TreeNode root = new TreeNode(55); root.left = new TreeNode(40); root.left.left = new TreeNode(21); root.left.left.right = new TreeNode(25); root.left.right = new TreeNode(44); root.left.right.left = new TreeNode(41); root.right = new TreeNode(100); root.right.left = new TreeNode(60); root.right.left.right = new TreeNode(70); root.right.right = new TreeNode(120); root.right.right.left = new TreeNode(110); root.right.right.right = new TreeNode(130); System.out.println(getMin(root)); System.out.println(getMax(root)); System.out.println(getLastSecondMin(root)); System.out.println(getLastSecondMax(root)); System.out.println(getLastSecondMinRecursion(root)); System.out.println(getLastSecondMaxRecursion(root)); printInOrder(root); System.out.println(); printPreOrder(root); System.out.println(); printPostOrder(root); System.out.println(); printTree(root); } public static int getMin(TreeNode root) { if (root == null) { throw new IllegalArgumentException("No elements"); } TreeNode current = root; while (current.left != null) { current = current.left; } return current.value; } public static int getMax(TreeNode root) { if (root == null) { throw new IllegalArgumentException("No elements"); } TreeNode current = root; while (current.right != null) { current = current.right; } return current.value; } public static int getLastSecondMin(TreeNode root) { if (root == null) { throw new IllegalArgumentException("No elements"); } TreeNode min = root; int parentMin = 0; TreeNode current = root; while (min.left != null) { parentMin = min.value; min = min.left; } if (min.right != null) { current = min.right; while (current.left != null) { current = current.left; } } else { return parentMin; } return current.value; } public static int getLastSecondMinRecursion(TreeNode root) { if (root == null || root.left == null) { throw new IllegalArgumentException("No elements"); } if (root.left.left == null && root.left.right == null) { return root.value; } if (root.left.left != null) { return getLastSecondMinRecursion(root.left); } return getMin(root.left.right); } public static int getLastSecondMax(TreeNode root) { if (root == null) { throw new IllegalArgumentException("No elements"); } TreeNode max = root; int parentMax = 0; TreeNode current = root; while (max.right != null) { parentMax = max.value; max = max.right; } if (max.left != null) { current = max.left; while (current.right != null) { current = current.right; } } else { return parentMax; } return current.value; } public static int getLastSecondMaxRecursion(TreeNode root) { if (root == null || root.right == null) { throw new IllegalArgumentException("No elements"); } if (root.right.right == null && root.right.left == null) { return root.value; } if (root.right.right != null) { return getLastSecondMaxRecursion(root.right); } return getMax(root.right.left); } public static void printInOrder(TreeNode root) { if (root != null) { printInOrder(root.left); System.out.print(root.value + " "); printInOrder(root.right); } } public static void printPreOrder(TreeNode root) { if (root != null) { System.out.print(root.value + " "); printPreOrder(root.left); printPreOrder(root.right); } } public static void printPostOrder(TreeNode root) { if (root != null) { printPostOrder(root.left); printPostOrder(root.right); System.out.print(root.value + " "); } } //breath first search - layer by layer //depth first search - put in value, pop out, check if children from right to left, pop out, check if children from right to left and on and on... }