[英]Finding the depth of a particular node in a tree using recursion
I'm currently trying to solve a problem where I return the maximum depth a number appears in a tree. 我正在尝试解决一个问题,我返回一个数字出现在树中的最大深度。 For example, if a tree looks like this:
例如,如果树看起来像这样:
1
/ \
2 3
\
2
My function should return 2. Yet, my function returns 0 我的函数应该返回2.但是,我的函数返回0
def max_depth(t,value):
if t == None:
return -1
left = max_depth(t.left, value)
right = max_depth(t.right, value)
if t.value == value:
return 1 + max(left,right)
else:
return max(left,right)
Is my thought process wrong? 我的思维过程错了吗? I should add
1
if the current value matches the one I'm looking for (which is the parameter), and do not add 1
if they do not match. 如果当前值与我正在查找的值(参数)匹配,我应该加
1
,如果它们不匹配则不加1
。 I use max()
so it returns the maximum of either the left child or the right child, so I get the child with the higher depth. 我使用
max()
所以它返回左子或右子的最大值,所以我得到了更高深度的孩子。 Is that wrong? 那是错的吗?
Here is the tree class: 这是树类:
class TN:
def __init__(self,value,left=None,right=None):
self.value = value
self.left = left
self.right = right
And here is my construction of the tree: 这是我对树的构造:
tree4 = TN(2)
tree3 = TN(3, left = None, right = tree4)
tree2 = TN(2)
tree1 = TN(1, left = tree2, right = tree3)
print(max_depth(tree1, 2))
That will print 0
那将打印
0
If I understand you correctly, the problem you're trying to solve is: what's the maximum depth of the value value
in the tree. 如果我理解正确的话,你要解决的问题是: 什么是价值的最大深度
value
在树中。
You should increase the count not only when t.value == value
, but also when any of the descendants of the tree matches the value you're looking for. 您应该增加计数不仅当
t.value == value
,而且当任何树的后裔,你要寻找的值相匹配。 This is because you're measuring the depth. 这是因为你正在测量深度。
Here's how the algorithm should look like: 以下是算法的外观:
def max_depth(t,value):
if t == None:
return -1
left = max_depth(t.left, value)
right = max_depth(t.right, value)
if t.value == value or left > -1 or right > -1: # <<<<
return 1 + max(left,right)
else:
return max(left,right) # This is always -1
I think this is a nice encoding of max_depth
我认为这是
max_depth
一个很好的编码
We add an additional parameter d
with a default value of 0
. 我们添加一个额外的参数
d
,默认值为0
。 This parameter is used to keep track of the current depth. 此参数用于跟踪当前深度。 When returning an answer, we only include
d
in the max (d, ...)
when the node t
value matches – otherwise, we return the max of the left
and right
results 当返回答案,我们只包括
d
在max (d, ...)
当节点t
值匹配-否则,我们返回的最大left
和right
结果
def max_depth (t, value, d = 0):
if t is None:
return -1
elif t.value == value:
return max ( d
, max_depth (t.left, value, d + 1)
, max_depth (t.right, value, d + 1)
)
else:
return max ( max_depth (t.left, value, d + 1)
, max_depth (t.right, value, d + 1)
)
Here's the tree from the code in your question 这是您问题中代码的树
tree = \
TN (1, TN (2), TN (3, right = TN (2)))
Find the max depth of each value 找到每个值的最大深度
print (max_depth (tree, 1))
# 0
print (max_depth (tree, 2))
# 2
print (max_depth (tree, 3))
# 1
In the event the value is never found, -1
will be returned 如果永远找不到值,将返回
-1
print (max_depth (tree, 4))
# -1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.