废话稍稍直接上代码
public class DoubleDirectNode<T> { T value = null; DoubleDirectNode<T> father; DoubleDirectNode<T> leftChild; DoubleDirectNode<T> rightChild;
}
public class Tree { public static int value = 0; /** * 根据深度构造二叉树 */ public static DoubleDirectNode createTree(DoubleDirectNode<Integer> father, int depth) { value++; if (depth == 0) return null; if (depth == 1) return new DoubleDirectNode(value); DoubleDirectNode res = new DoubleDirectNode(value); int d2 = depth - 1; res.father = father; res.leftChild = Tree.createTree(res, d2); res.rightChild = Tree.createTree(res, d2); return res; } public static void main(String[] args) { DoubleDirectNode root = new DoubleDirectNode(1); DoubleDirectNode tree = Tree.createTree(root, 3); System.out.println(tree.toString()); } }