我正在尝试查找二叉搜索树中的最大值。给出了样例解决方案,但我想知道我的解决方案是否有问题?我对样例解决方案的问题在于它似乎两次检查每个节点是否为“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
root_node
是None
(也就是说,我们得到了一棵空树),会发生什么? - kirelagin