4

目的:编写一个函数来连接二叉树中同一级别的所有相邻节点。给定二叉树节点的结构如下所示。

struct node{
  int data;
  struct node* left;
  struct node* right;
  struct node* nextRight;  
}

最初,所有 nextRight 指针都指向垃圾值。函数应将这些指针设置为每个节点的右下角。

解决方案:

void connectRecur(struct node* p);
struct node *getNextRight(struct node *p);

// Sets the nextRight of root and calls connectRecur() for other nodes
void connect (struct node *p)
{
    // Set the nextRight for root
    p->nextRight = NULL;

    // Set the next right for rest of the nodes (other than root)
    connectRecur(p);
}

/* Set next right of all descendents of p. This function makes sure that
nextRight of nodes ar level i is set before level i+1 nodes. */
void connectRecur(struct node* p)
{
    // Base case
    if (!p)
       return;

    /* Before setting nextRight of left and right children, set nextRight
    of children of other nodes at same level (because we can access 
    children of other nodes using p's nextRight only) */
    if (p->nextRight != NULL)
       connectRecur(p->nextRight);

    /* Set the nextRight pointer for p's left child */
    if (p->left)
    {
       if (p->right)
       {
           p->left->nextRight = p->right;
           p->right->nextRight = getNextRight(p);
       }
       else
           p->left->nextRight = getNextRight(p);

       /* Recursively call for next level nodes.  Note that we call only
       for left child. The call for left child will call for right child */
       connectRecur(p->left);
    }

    /* If left child is NULL then first node of next level will either be
      p->right or getNextRight(p) */
    else if (p->right)
    {
        p->right->nextRight = getNextRight(p);
        connectRecur(p->right);
    }
    else
       connectRecur(getNextRight(p));
}

/* This function returns the leftmost child of nodes at the same level as p.
   This function is used to getNExt right of p's right child
   If right child of p is NULL then this can also be used for the left child */
struct node *getNextRight(struct node *p)
{
    struct node *temp = p->nextRight;

    /* Traverse nodes at p's level and find and return
       the first node's first child */
    while(temp != NULL)
    {
        if(temp->left != NULL)
            return temp->left;
        if(temp->right != NULL)
            return temp->right;
        temp = temp->nextRight;
    }

    // If all the nodes at p's level are leaf nodes then return NULL
    return NULL;
}

这个解决方案的时间复杂度是多少?

4

3 回答 3

1

由于 getNextRight,它是 O(n^2)。

最容易看到的是考虑你有一个完整的二叉树。叶子的数量是 O(n/2) 所以 O(n)。您可以为每个叶子调用 getNextRight。

第一个 getNextRight 将用于右侧的最后一片叶子。这不需要通过while循环。

接下来,您调用 getNextRight 来获取右侧的倒数第二个叶子。这需要 1 次通过 while 循环。

对于下一个叶子,您将通过 while 循环获得 2 次。依此类推……你得到 O(1 + 2 + 3 + ... + n/2),即 O(n^2)。

此外,空间复杂度并不是真正恒定的。如果树由于递归而平衡,则为 O(log n)。您可能有一个 log n 大小的堆栈。如果树不平衡,那将是最糟糕的。

于 2013-10-19T12:01:33.497 回答
0

在我看来,您正在从根节点开始逐级连接节点。

在任何级别,由于父母已经连接,您只需通过父母联系下一个分支。因此,您最多只能访问每个节点一次(或通过子节点访问两次)。

因此对我来说,时间复杂度的顺序似乎是 O(n),其中 n 是树中元素的总数。

于 2013-10-19T11:08:22.340 回答
0

我使用级别顺序遍历在stackoverflow上提供了一个类似问题的答案。空间和时间复杂度为 O(n)

于 2021-02-18T23:34:41.917 回答