3031323334353637
public class Q049B_Construct_BST_from_given_preorder_traversal { public static void main(String[] args) { int[] pre = {10, 5, 1, 7, 40, 50}; TreeNode root = constructBST(pre); root.print(); }
5556575859606162636465
if (key <= min || key >= max) { return null; } TreeNode root = new TreeNode(key); index++; root.left = helper(pre, min, key); root.right = helper(pre, key, max);
212223242526272829
import adt.tree.TreeNode; public class Q016_Diameter_of_a_Binary_Tree { public static void main(String[] args) { TreeNode root = Tree.bst2(); int d = diameter(root); System.out.println(d); }
525354555657585960
import adt.tree.TreeNode; public class Q021_Double_Tree { public static void main(String[] args) { TreeNode root = Tree.bst2(); doubleTree(root); root.print(); }
666768697071727374
/* Make double subtrees */ doubleTree(root.left); doubleTree(root.right); TreeNode left = root.left; root.left = new TreeNode(root.val); root.left.left = left; }
656667686970717273
import adt.tree.TreeNode; public class Q037_Find_the_largest_BST_subtree_in_a_given_Binary_Tree { public static void main(String[] args) { TreeNode root = Tree.tree6(); largestBST(root); System.out.println(max_bst); }
222324252627282930
import adt.tree.TreeNode; public class Q028_Get_Level_of_a_node_in_a_Binary_Tree { public static void main(String[] args) { TreeNode root = Tree.bst2(); int level = getLevel(root, 10); System.out.println(level); }
26272829303132333435
import adt.tree.TreeNode; public class Q041A_Merge_Two_Balanced_Binary_Search_Trees { public static void main(String[] args) { TreeNode r1 = Tree.bst1(); TreeNode r2 = Tree.bst2(); TreeNode root = mergeBST(r1, r2); root.print(); }
404142434445464748
inorder(r1, l1); inorder(r2, l2); ArrayList<Integer> l = mergeLists(l1, l2); TreeNode root = sortedArrayToBST(l, 0, l.size() - 1); return root; }
8990919293949596979899
return null; } int mid = lo + (hi - lo) / 2; TreeNode root = new TreeNode(list.get(mid)); root.left = sortedArrayToBST(list, lo, mid - 1); root.right = sortedArrayToBST(list, mid + 1, hi); return root;