【问题标题】:Data Structure - Tree - what this function doing数据结构 - 树 - 这个函数在做什么
【发布时间】:2014-02-19 19:58:49
【问题描述】:

我有这两个功能:

// Return the smallest number
public static int min (int a, int b) { ... }

// Return is the Node is Leaf
public static boolean isLeaf (Node t) { ... }

这个函数有什么作用:

public static int f (Node t)
{
   if (t == null)
   return 0;
   return 1 + min (f (t.getLeftSon()),f (t.getRightSon()));
}

【问题讨论】:

  • 你认为它有什么作用?在家工作?对我来说,它看起来像是最低的子树高度。
  • 尝试画一棵树,看看这个递归方法的行为和作用。
  • 我已经尝试过了,但是递归非常混乱

标签: java function recursion tree


【解决方案1】:

此方法递归遍历由Node 对象表示的二叉树,并返回其最小子树的高度。如果树只有一个根,它将返回1

【讨论】:

    【解决方案2】:

    这个函数递归地调用自己,当它的值变为空时它会停止。然后,迭代地回调并计算一些东西。这个计算的最大高度是三个..

    【讨论】:

    • 究竟如何通过调用min来涉及最大的任何事情?
    • 这个通用描述究竟是如何回答这个问题的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2020-11-04
    • 2018-04-19
    相关资源
    最近更新 更多