递归计算二叉树的高度_如何使用递归方法计算二叉树的高度

递归计算二叉树的高度

Previously I wrote about an algorithm for finding out the height of a binary tree using iteration. Though that method gets the job done (in 7 steps no less), the same can be accomplished in a much simpler way.

以前,我写过一篇关于使用迭代找出二叉树的高度的算法 。 尽管该方法可以完成工作(分不少于7个步骤),但可以用更简单的方法完成相同的工作。

In my opinion, one of the most powerful programming techniques is recursion. For readers new to programming — it is simply a function or method calling itself. To make the introduction simpler we have a method below that calls another method:

在我看来,递归是最强大的编程技术之一。 对于刚接触编程的读者来说,它只是一个调用自身的函数或方法。 为了使介绍更简单,我们在下面有一个调用另一个方法的方法:

def outer_method(name)       (R1)
  inner_method + name
end
def inner_method             (R2)
  "Hello "
end
print outer_method("Steve") -> #"Hello Steve"

In the above method outer_method, which takes in a string as argument, calls inner_method, which simply returns the string “Hello “ inside it. Recursion is similar in that, say in this case, outer_method simply calls itself:

在上面的方法outer_method ,它以字符串作为参数,调用inner_method ,该方法只是在其中返回字符串“Hello “ 。 递归相似之处在于,在这种情况下, outer_method简单地调用自身:

def outer_method(name)              (R3)
  outer_method("hello ") + name
end (R3)

One caveat, though, with code R3 above — it will run until the computer complains that resources are not enough to keep processing the method. It’s like running an infinite loop except that infinite loops don’t necessarily raise exceptions. The reason for this is that code R3 doesn’t have a ‘terminal state’ or a point where it doesn’t ‘recurse’ anymore.

需要注意的是,上面的代码为R3它会一直运行,直到计算机抱怨资源不足以继续处理该方法为止。 这就像运行一个无限循环,只是无限循环不一定会引发异常。 原因是代码R3没有“终端状态”或不再“递归”的点。

We can solve this by including a terminal state:

我们可以通过包含终端状态来解决此问题:

def outer_method(name)                 (R4)
  return name if name == "hello "
  outer_method("hello ") + name
end

The first line inside the method definition simply states that if the argument name is equal to ‘hello’ then simply return name. That will then ignore any line after it. Therefore in the second line, the code outer_method(“hello “) will simply give the string “hello “ to be added to whatever name is in the main argument. So the same print outer_method(“Steve”) will result in the output “hello Steve” as well.

方法定义中的第一行只是指出,如果参数name等于'hello'则只需返回name 。 然后,它将忽略其后的任何行。 因此,在第二行中,代码outer_method(“hello “)将简单地将字符串“ hello”添加到主参数中的任何名称上。 因此,相同的print outer_method(“Steve”)也会导致输出“hello Steve”

OK then, that may not be the best example for describing recursion (as the recursive version in this case doesn’t have that much advantage over the non-recursive one). But working on the binary tree height problem, we will see that recursion is so much simpler to understand and faster to run.

好的,那可能不是描述递归的最佳示例(因为在这种情况下,递归版本与非递归版本相比没有太多优势)。 但是在处理二叉树高度问题时,我们将看到递归非常容易理解并且运行起来更快。

For this discussion let me put again the same example as I showed in the previous article:

对于此讨论,让我再次输入与上一篇文章相同的示例:

which we can represent as the following array:

我们可以将其表示为以下数组:

tree = [1, 7, 5, 2, 6, 0, 9, 3, 7, 5, 11, 0, 0, 4, 0] (T0)

The indices of the left and right children of a

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值