链表的递归:递归关系和递归树

时间:2018-03-24 21:18:33

标签: c++ recursion linked-list big-o recurrence

我必须写一个递归关系来计算单链表中的节点数。我已经这样做了,代码可以在下面找到。在编写程序之后,我将提供程序的递归关系程序的递归树方法。因为我收到的复发关系是T(n)= T(n-1)+ O(1),我认为是正确的。我很困惑如何为此构建一个重复树,我想知道我是否能得到一些帮助。谢谢。

代码:

int Length(struct Node* head) //must start at the head of the LL
{
if (head == NULL) //base case for the recursion
{ 
    return 0;
}
return 1 + Length(head->next);
}

0 个答案:

没有答案