gpt4 book ai didi

algorithm - Find Lowest Common Ancestor 代码解释

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:52 24 4
gpt4 key购买 nike

我正在查看 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() );
}

最佳答案

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

关于algorithm - Find Lowest Common Ancestor 代码解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14046161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com