【问题标题】:Java Simple Binary Tree check equalsJava 简单二叉树检查等于
【发布时间】:2021-02-03 02:27:44
【问题描述】:

我不明白为什么这个equals 函数不起作用:

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
    
    public boolean equals(TreeNode other) {
        if (this == null && other == null) {
            return true;
        } else if (this == null || other == null) {
            return false;
        } else {
            return this.val == other.val
                    && this.left.equals(other.left)
                    && this.right.equals(other.right);
        }
    }
}

似乎主要问题是我无法比较 null TreeNode,但在设置中我已经指出了如何处理 null?

TreeNode a = new TreeNode(5);
TreeNode b = new TreeNode(5);
System.out.println(a.equals(b)); // >>> NullPointerException

上面的比较是从非空开始的,但最终当分支到空左或空右时它会命中空。一种工作方法是将上述相同的方法提取到静态方法中:

public static boolean isEqual(TreeNode self, TreeNode other) {
    if (self == null && other == null) {
        return true;
    } else if (self == null || other == null) {
        return false;
    } else {
        return self.val == other.val
                && isEqual(self.left, other.left)
                && isEqual(self.right, other.right);
    }
}

这样可以正常工作:

TreeNode a = new TreeNode(5);
TreeNode b = new TreeNode(5);
System.out.println(TreeNode.isEqual(a, b)); // >>> true

下面也可以,避免this.left/right成为null,看起来很愚蠢,但它是java

public boolean equals(TreeNode other) {

    if (this.left == null && other.left == null && this.right == null && other.right == null) {
        return this.val == other.val;
    } else if (this.left != null && other.left != null && this.right != null && other.right != null) {
        return this.val == other.val && this.left.equals(other.left) && this.right.equals(other.right);
    } else if (this.left != null && other.left != null && this.right == null && other.right == null) {
        return this.val == other.val && this.left.equals(other.left);
    } else if (this.left == null && other.left == null && this.right != null && other.right != null) {
        return this.val == other.val && this.right.equals(other.right);
    } else {
        return false;
    }
}

【问题讨论】:

  • a.equals(....),当您调用它时,a 到底是什么?提示:它不是不为空的东西。
  • aTreeNodenull?
  • 对,在null 上调用方法会导致?
  • 在 python 中,确切的代码可以正常运行。你能建议我应该如何避免这种情况吗?
  • 是的,因为它是equals(Object)That equals 方法是在 Set 和其他容器类中使用的方法。因此,代码有点误导/混淆。这就是我想要表达的全部内容。

标签: java data-structures binary-tree equals


【解决方案1】:

a 为空。因此,调用“equals”将立即引发 NullPointerException,而不会调用 equals 方法。

两个可能让你困惑的问题:

  1. 在第一个实现中,检查“this==null”是多余的(在这种情况下 this 不能为 null)。
  2. “this.left”和“this.right”空值检查也是多余的,因为它们是原语,永远不能为空。

在我看来,应该是这样的:

public boolean equals(TreeNode other) {
    if (other == null) {
        return false;
    }

    // Different Values
    if (this.val != other.val) {
        return false;
    }

    if (this.left == null && other.left != null) {
        return false;
    }
    if (this.left != null && !this.left.equals(other.left)) {
        return false;
    }
    
    if (this.right == null && other.right != null) {
        return false;
    }
    if (this.right != null && !this.right.equals(other.right)) {
        return false;
    }

    return true;
}

【讨论】:

  • @jxie0755 他们为什么要写代码?在 java 中,如果对象为 null,则非静态方法将不起作用。那么为什么要检查this == null
  • 在上面的答案中添加了它。
  • 谢谢,我觉得你的代码看起来好多了,而且我知道this 永远不可能是null。由于继承,此问题仅适用于 .equals。如果我创建了一个静态方法,这段代码就可以正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 2021-02-26
  • 1970-01-01
  • 2022-01-27
相关资源
最近更新 更多