擅长:python、mysql、java
<p>您可以从数组中创建字典(子级到父级),并使用<code>while</code>循环方法:</p>
<pre><code>data = [['Mary', 'Dan'], ['Dan', 'Steven'], ['Steven', 'Craig'], ['Janice', 'Keith']]
tree = {child: parent for child, parent in data}
def oldest_ancestor(child):
parent = child
while tree.get(parent, None) is not None:
parent = tree[parent]
return parent
print(oldest_ancestor('Mary')) # Craig
</code></pre>