Arrays 数组中二叉树的存储元素
我想将二叉树中的元素存储到数组中,但不知道如何做。。。 我已经有了一个数组大小的计数方法,现在我需要将int元素存储到数组中Arrays 数组中二叉树的存储元素,arrays,algorithm,recursion,binary-tree,Arrays,Algorithm,Recursion,Binary Tree,我想将二叉树中的元素存储到数组中,但不知道如何做。。。 我已经有了一个数组大小的计数方法,现在我需要将int元素存储到数组中 public int countNode() { if (this.root == null) { return 0; } else { int count = 1; count += root.l.countNode(); count += root.r.countNode();
public int countNode() {
if (this.root == null) {
return 0;
} else {
int count = 1;
count += root.l.countNode();
count += root.r.countNode();
return count;
}
}
public int[] arrayStorage() {
int[] a = new int[countNode()];
}
您可以通过使用队列遍历树,按级别顺序将其添加到数组中 对于二叉树,我们不采用这种方法的主要原因是,与堆不同,当二叉树不完整时,这会在数组中浪费大量空间