【发布时间】:2015-05-11 04:55:11
【问题描述】:
我试图在二叉搜索树中找到最大值。下面给出了示例解决方案,但我试图了解我的解决方案是否有问题?我对示例解决方案的问题是,它似乎检查每个节点是否不是 None 两次:一次在“if not current.right”中,第二次在“while current ... current = current.right”中,这似乎是多余的。
示例解决方案:
def find_largest(root_node):
current = root_node
while current:
if not current.right:
return current.value
current = current.right
我的解决方案:
def find_largest(root_node):
current = root_node
if current:
while current.right is not None:
current = current.right
return current.value
问题/代码来源:Interviewcake.com
【问题讨论】:
标签: python binary-tree binary-search-tree