내 문제는 트리에서 value
노드의 깊이를 반환하도록 요청하고 있습니다. 내가 depth(root, 7, 0 [depth initially])
을 한 경우에값 이진 검색 트리가있는 특정 노드의 깊이를 반환
예를 들어, 2.
내 첫 번째 시도를 반환해야합니다,이
# value is the value wanted, count measures the 'depth'
def depth(root, value, count):
# if we are not at a empty node
if root != None:
# if we found our data, then just return the count (depth)
if root.data == value:
return count
# otherwise increase count, and traverse both sides
else:
count += 1
count = depth(root.left, value, count)
count = depth(root.right, value, count)
return count
같은 내가 깊이를 얻을 수 있지만 내가 이것을 실행했다 = 6이고 확실하지 않습니다.
죄송합니다. 실수로 저를 대신해서 실수로 코드를 복사 한 것처럼 보였습니다. 내가 편집했습니다. –