我正在尝试编写一个方法,如果二叉树已满(每个节点有2个子节点或没有),则返回true,否则返回false。这在一些时间工作,但不是全部。关于我哪里出错的任何建议?
public static void testNum4()
{
System.out.println("How many nodes do you want in your tree?");
int num=sc.nextInt();
//TreeNode<Integer> root = TreeUtil.createBalancedNumberTree(num); Use to test for a balanced tree
TreeNode<Integer> root = TreeUtil.createIntegerTree(num);
TreeUtil.displayTreeInWindow(root);
System.out.println(isFull(root));
TreeUtil.displayTreeInWindow (root);
}
public static boolean isFull(TreeNode<Integer> root) {
// pre: root of tree, 0 or more nodes
// post: returns true if the input tree is a full tree; false otherwise
if (root!=null) {
if ((root.getLeft() != null && root.getRight() != null) || (root.getRight() == null && root.getLeft() == null))
{
return true;
}
else if (root.getLeft()!=null)
{
isFull(root.getLeft());
}
else if (root.getRight()!=null)
{
isFull(root.getRight());
}
else
return false;
}
return false;
}
答案 0 :(得分:3)
定义:如果每个节点都是一个叶子或者只拥有两个子节点,则二叉树T已满。
public static boolean isFull(TreeNode<Integer> root)
// pre: root of tree, 0 or more nodes
// post: returns true if the input tree is a full tree; false otherwise
{
if (root!=null)
{
if(root.getRight() == null && root.getLeft() == null)
{
return true;
}
if ((root.getRight() != null && root.getLeft() != null))
{
return isFull(root.getLeft())&&isFull(root.getLeft());
}
}
return false;
}
答案 1 :(得分:1)
尝试将return添加到每个语句。
else if (root.getLeft()!=null && root.getRight()!=null)
{
return isFull(root.getLeft()) && isFull(root.getRight());
}
此外,如果根节点为null,则表示树已满。所以最后一次返回应该是return true;
答案 2 :(得分:1)
问题是else if
和缺少return
语句。也不需要检查null
这么多,并且使用方法使其更具可读性。
public static boolean isFull(TreeNode<Integer> node) {
if (node == null) return false;
if (isLeaf(node)) return true;
return isFull(node.getLeft()) && isFull(node.getRight());
}
public static boolean isLeaf(TreeNode<Integer> node) {
return node.getRight() == null && node.getLeft() == null;
}
答案 3 :(得分:1)
在这种情况下,所有上述算法都返回true(因为他们不应该): complete binary tree 。所以,希望这会有所帮助:
Public Class MainForm
Structure Members
Public id As String
Public lastName As String
Public firstName As String
Public middleInitial As String
Public grade As String
Public classPeriod As String
End Structure
'declare an array of Members
Private mem(250) As Members
Dim totalElements As Integer = 0
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadStudentFile()
WriteStudentFile()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub LoadStudentFile()
Dim inFile As IO.StreamReader
inFile = IO.File.OpenText("StudentList.txt")
Dim tempArray(5) As String
Dim subscript As Integer = 0
Do Until inFile.Peek = -1
tempArray = inFile.ReadLine.Split(","c)
mem(subscript).id = tempArray(0)
mem(subscript).lastName = tempArray(1)
mem(subscript).firstName = tempArray(2)
mem(subscript).middleInitial = tempArray(3)
mem(subscript).grade = tempArray(4)
mem(subscript).classPeriod = tempArray(5)
subscript = subscript + 1
totalElements = totalElements + 1
Loop
inFile.Close()
End Sub
Private Sub WriteStudentFile()
For i As Integer = 0 To totalElements - 1
StudentsList.Items.Add(mem(i).id.PadLeft(10, "0") & " " & mem(i).lastName & ", " & mem(i).firstName & " " & mem(i).middleInitial)
Next
End Sub
答案 4 :(得分:0)
您没有完全遍历树。使用递归来命中所有节点。检查根节点。如果没有孩子,则返回true。如果有孩子,请确保有两个孩子,然后递归检查每个孩子。
答案 5 :(得分:0)
我认为if语句应该如下:
if (root.getRight() == null && root.getLeft() == null)
{
// The node has no children (full)
return true;
}
else if (root.getLeft() != null && root.getRight() != null)
{
// There are two children. Tree is only full if both sub trees are full
return isFull(root.getLeft()) && isFull(root.getRight());
}
else
{
// Only one child
return false;
}