Python:二叉树中的最低共同祖先,对CodeEval的挑战
首先,我知道还有很多其他线程处理这个问题。我想知道的是,当我在CodeEval上提交时,为什么我的解决方案似乎没有100%排名。它的排名仅为70%。当我在本地环境中测试它时,它可以很好地适用于我抛出的所有测试情况 我知道我的解决方案不是最有效的。我试图做的是自己想出一个解决办法,一个我能理解的办法。我最终会将其与其他人进行比较。但在此之前,有人能解释一下我做错了什么吗Python:二叉树中的最低共同祖先,对CodeEval的挑战,python,tree,lowest-common-ancestor,Python,Tree,Lowest Common Ancestor,首先,我知道还有很多其他线程处理这个问题。我想知道的是,当我在CodeEval上提交时,为什么我的解决方案似乎没有100%排名。它的排名仅为70%。当我在本地环境中测试它时,它可以很好地适用于我抛出的所有测试情况 我知道我的解决方案不是最有效的。我试图做的是自己想出一个解决办法,一个我能理解的办法。我最终会将其与其他人进行比较。但在此之前,有人能解释一下我做错了什么吗 class binary_tree(object): def __init__(self, value=None, left=N
class binary_tree(object):
def __init__(self, value=None, left=None, right=None):
self.value=value
self.left=left
self.right=right
def insert(self, num):
#print "...now:", num
if self.value==num:
return
else:
#print "descending from", self.value
if num<self.value:
if self.left:
#print "descending to left"
self.left.insert(num)
else:
#print "! found empty left"
self.left=binary_tree(num)
else:
if self.right:
#print "descending to right"
self.right.insert(num)
else:
#print "! found empty right"
self.right=binary_tree(num)
def tree_from_list(self, value, numbers):
self.value=value
for num in numbers:
self.insert(num)
def __repr__(self,depth=0):
rtn=""
rtn+="\t"*depth+" "+str(self.value)+"\n"
depth+=1
if self.left:
rtn+="\t"*depth+" left: "
rtn+=self.left.__repr__(depth)
if self.right:
rtn+="\t"*depth+" right: "
rtn+=self.right.__repr__(depth)
return rtn
def find_parent_depth(self, num, depth=0):
if self.left and self.left.value==num:
#returns a list of two values, the first one being
#the depth of the parent, and the second the value
#itself. depth starts at 0, and increases as we descend
return [depth, self.value]
elif self.right and self.right.value==num:
return [depth, self.value]
else:
depth+=1
#checks for which path to descend on
if num<self.value:
if self.left:
return self.left.find_parent_depth(num, depth)
else:
return self.value
else:
if self.right:
return self.right.find_parent_depth(num, depth)
else:
return self.value
#lca = lowest common ancestor
def lca(self, v1, v2):
parent1=self.find_parent_depth(v1)
parent2=self.find_parent_depth(v2)
#checks for which parent has lower depth
if parent1[0]<parent2[0]:
#searches for ancestors till it reaches the same depth level
while parent1[0]!=parent2[0]:
parent2=self.find_parent_depth(parent2[1])
#checks if the ancestors coincide at that depth
if parent1[1]==parent2[1]:
#if they do, returns the parent value
#THIS IS IT
return parent1[1]
#if it doesn't, we need to raise the level of the lowest one
#and do it all over again
else:
return self.lca(parent1[1], parent2[1])
else:
#searches for ancestors till it reaches the same depth level
while parent2[0]!=parent1[0]:
parent1=self.find_parent_depth(parent1[1])
#checks if the ancestors coincide at that depth
if parent1[1]==parent2[1]:
#if they do, returns the parent value
#THIS IS IT
return parent1[1]
#if it doesn't, we need to raise the level of the lowest one
#and do it all over again
else:
return self.lca(parent2[1], parent1[1])
dis=binary_tree()
dis.tree_from_list(30, [8, 52, 3, 20, 10, 29, 12, 90, 89, 1])
print dis.lca(89, 12)
类二叉树(对象):
定义初始化(self,value=None,left=None,right=None):
自我价值=价值
self.left=左
self.right=right
def插入(自我,数值):
#打印“…现在:”,num
如果self.value==num:
回来
其他:
#打印“从”,self.value
如果num我也有类似的问题。鉴于:
30
_|___
| |
8 52
_|___
| |
3 20
_|___
| |
10 29
我的问题与生命周期评价的定义有关
我发现20和29的最低共同祖先是20,而不是我最初认为的方向中给出的例子中的8。另一个例子是LCA为8,20为8
调整代码后,我的提交通过了100%
希望这有帮助。干杯 我认为有一个特定的测试案例,在这个案例中,这个解决方案失败了。我想不出有什么,CodeEval也不显示他们用作测试的输入。根据定义,LCA(30,8)=>30。你的代码在那里崩溃了。那么,当其中一个输入值是30,即没有父节点的最高节点时,代码应该返回什么?