0

我正在查看 Programming Interviews Exposed 中的以下代码,我似乎无法理解它是如何工作的。这个方法不会总是返回null吗?

// Overload it to handle nodes as well
Node findLowestCommonAncestor( Node root, Node child1,
                               Node child2 ){
    if( root == null || child1 == null || child2 == null ){
        return null;
    }

    return findLowestCommonAncestor( root, child1.getValue(),
                                     child2.getValue() );
}
4

1 回答 1

2

从代码片段中,我们真的不知道 getValue 返回什么。因此,如果存在 findLowestCommonAncestor 的其他重载版本,并且 getValue 返回 Node 以外的其他内容,则代码段中对 findLowestCommonAncestor 的调用不会递归调用自身。

于 2012-12-26T22:53:36.033 回答