public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; TreeNode root = TreeNode.createMinimalBST(array); TreeNode n3 = root.find(10); TreeNode n7 = root.find(6); TreeNode ancestor = commonAncestor(root, n3, n7); if (ancestor != null) { System.out.println(ancestor.data); } else { System.out.println("null"); } }
public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; TreeNode root = TreeNode.createMinimalBST(array); TreeNode n3 = root.find(1); TreeNode n7 = root.find(7); TreeNode ancestor = commonAncestor0(root, n3, n7); System.out.println(ancestor.data); }
public static void main(String[] args) { int[] array = {5, 3, 6, 1, 9, 11}; TreeNode root = new TreeNode(20); for (int a : array) { root.insertInOrder(a); } TreeNode n1 = root.find(1); TreeNode n9 = root.find(9); TreeNode ancestor = commonAncestor(root, n1, n9); System.out.println(ancestor.data); }
// Test public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; TreeNode root = TreeNode.createMinimalBST(array); for (int i = 0; i < array.length; i++) { TreeNode node = root.find(array[i]); TreeNode next = nextNode(node); if (next != null) { System.out.println(node.data + "->" + next.data); } else { System.out.println(node.data + "->" + null); } } }