如果树中的所有叶子都等于其父项,则返回true的操作

时间:2017-03-25 19:30:36

标签: c# tree binary-tree

嗨我试过这个但是它没有用,我需要编写操作,如果树中的所有叶子都等于它们的父值,则返回true,请帮助我测试,也许这是一个主要问题?

主:

static void Main(string[] args)
    {
        BinNode<int> t1, t2, bt1, bt2, bt;

        t1 = new BinNode<int>(3);
        t2 = new BinNode<int>(3);
        t2.SetLeft(new BinNode<int>(null, 3, new BinNode<int>(3)));
        bt1 = new BinNode<int>(t1, 3, t2);
        bt1 = new BinNode<int>(null, 3, bt1);
       t1 = new BinNode<int>(3);
        t2 = new BinNode<int>(3);
        t2.SetLeft(new BinNode<int>(3));
        t2.SetRight(new BinNode<int>(new BinNode<int>(3), 3, null));
        bt2 = new BinNode<int>(t1, 3, t2);
        bt = new BinNode<int>(bt1, 3, bt2);

        Console.WriteLine(SumTree(bt));
        Console.WriteLine(LeafCounter(bt));
        Console.WriteLine(CountWhoHasTwoSameSons(bt));
        Console.WriteLine(IsLeafEqualHisFather(bt));
        Console.ReadLine();
    }

操作:

 public static  bool IsLeafEqualHisFather(BinNode<int> Head)
    {
        if (Head != null)
        {
            if (IsLeaf(Head))
                return true;
            if (IsLeaf(Head.GetRight()) == true && (Head.GetRight().GetValue() == Head.GetValue()) || IsLeaf(Head.GetLeft()) == true && (Head.GetLeft().GetValue() == Head.GetValue()))
            {
                return IsLeafEqualHisFather(Head.GetRight()) && IsLeafEqualHisFather(Head.GetLeft());
            }

        }
        return false;
    }

2 个答案:

答案 0 :(得分:0)

基本上,您需要首先检查给定节点是null还是叶子。如果不是,则需要确定左侧和右侧节点是否为空,如果不是,则确定它们是叶子还是子树。如果它们是叶子,那么你需要将值与头部进行比较,或者如果它们是子树,则需要以递归方式调用子树上的方法。

public static  bool AllLeavesEqualToParent(BinNode<int> Head)
{
    if (Head == null)
        return false; // no leaves and no parent.

    if (IsLeaf(Head))
        return false; // head is a leaf, so there is no parent.

    var right = head.GetRight();
    var left = head.GetLeft();
    if(left != null) 
    {
        if (IsLeaf(left))
        {
            if (left.GetValue() != Head.GetValue())    
                return false; // left leaf didn't match
        }
        else if(!AllLeavesEqualToParent(left))
            return false; // left sub tree has leaf that doesn't match parent.
    }

    if(right!= null) 
    {
        if (IsLeaf(right))
        {
            if (right.GetValue() != Head.GetValue())    
                return false; // right leaf didn't match
        }
        else if(!AllLeavesEqualToParent(right))
            return false; // right sub tree has leaf that doesn't match parent.
    }

    return true; //Both left and right are either null (but not both), a leaf that matches
                 // the head, or sub trees with all leaves that match their parents.
}

答案 1 :(得分:0)

看起来这里的主要问题是,如果整个大if表达式的计算结果为true,则只执行递归检查。但是只有你已经看过树叶才会出现这种情况,所以如果你从一棵大树的顶端开始,你就会看不出来。

让我们简化它 - 检查左边是否通过了测试。这意味着以下之一: 1.左边节点是null 2.左节点是叶子,并且具有与当前节点相同的值 3.左节点不是叶子并通过IsLeafEqualHisFather测试。

右侧重复相同的操作。然后检查它们是否都通过了。

旁注:(几乎)永远不会使用[something] == true[something]本身就是为了这个目的。

public static  bool IsLeafEqualHisFather(BinNode<int> current)
{
    if (current != null)
    {
        if (IsLeaf(current))
        {
            return true;
        }

        var currentValue = current.GetValue();

        var left = current.GetLeft();
        var leftPasses = left == null || 
            IsLeaf(left) ? left.GetValue() == currentValue : IsLeafEqualHisFather(left);

        var right = current.GetRight();
        var rightPasses = right == null || 
            IsLeaf(right) ? left.GetValue() == currentValue : IsLeafEqualHisFather(left));

        return leftPasses && rightPasses;            
    }

    return false;
}