java 实现二叉树的层次遍历

来源:百度知道 编辑:UC知道 时间:2024/06/08 15:52:04
任意选定一棵至少含有8个结点的二叉树,编程实现:
(1)按层次遍历算法遍历该二叉树,并给出遍历结果;
(2)利用层次遍历算法显示所有叶结点。关键是要显示所有叶结点以及最后能有这个程序的界面图形代码
拜托哪位达人帮帮忙,救命的...

class TreeNode {
public TreeNode left;

public TreeNode right;

public int value;

public TreeNode(TreeNode left, TreeNode right, int value) {
this.left = left;
this.right = right;
this.value = value;
}
}
public class BinaryTree {

public static int getTreeHeight(TreeNode root) {
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 1;
return 1 + Math
.max(getTreeHeight(root.left), getTreeHeight(root.right));
}

public static void recursePreOrder(TreeNode root) {
if (root == null)
return;
System.out.println(root.value);
if (root.left != null)
recursePreOrder(root.left);
if (root.right != null)
recursePreOrder(root.right)