public static TreeNode createBinaryTree(int[] values) { TreeNode root = new TreeNode(); root.value = values[0]; for (int i = 1; i < values.length; i++) { insert(root, values[i], root); } return root; }
public static void insert(TreeNode tnode, int x, TreeNode p) { if (tnode == null) { tnode = new TreeNode(); tnode.value = x; tnode.lchild = null; tnode.rchild = null; if (p.value > x) p.lchild = tnode; else p.rchild = tnode; tnode.parent = p; return; } if (x < tnode.value) { insert(tnode.lchild, x, tnode); } else { insert(tnode.rchild, x, tnode); } }