【问题标题】:In a BST two nodes are randomly swapped we need to find those two nodes and swap them back在 BST 中,两个节点是随机交换的,我们需要找到这两个节点并将它们交换回来
【发布时间】:2012-08-06 08:38:21
【问题描述】:

给定一个二叉搜索树,其中任意两个节点被交换。所以我们需要找到这两个节点并将它们交换回来(我们需要交换节点,而不是数据)

我试图通过创建一个额外的数组来做到这一点,该数组具有树的中序遍历并保存指向每个节点的指针。 然后我们只是遍历数组,找到没有排序顺序的两个节点,现在这两个节点在树中搜索然后交换

所以我的问题是如何在 O(1) 空间中解决这个问题?

【问题讨论】:

  • 参考this。它实际上只使用了另外三个指针。

标签: algorithm binary-search-tree


【解决方案1】:

In order traversal 在 BST 上为您提供对元素的遍历,有序。
您可以使用中序遍历,找到两个不合适的元素(将它们存储在两个指针中)并将它们交换回来。

此方法实际上是动态创建您描述的数组,而不是实际创建它。

但是请注意,由于堆栈跟踪,空间消耗不是O(1),而是O(h),其中h 是树的高度。如果树是平衡的,它将是O(logn)

【讨论】:

  • 如果你从树中创建一个包含所有n 元素的数组,你如何得到O(h) 的空间复杂度?
  • @SalvadorDali This method is actually creating the array you described on the fly, **without actually creating it.**
  • 感谢您的快速答复,但我已阅读。而这种神奇的文字组合并不是很清楚。尤其是当一个解决方案很明显可以是“进行中序遍历并在数组中找到交换的元素”时。现在我阅读了您的解决方案,它告诉您相同的内容,添加而不实际创建它。所以它以某种方式暗示我不需要创建它,但不是我该怎么做。希望我的观点很清楚。
  • @SalvadorDali 在进行中序遍历时,您会得到一个 stream 元素。在每一点,您只需要存储:元数据(由于堆栈跟踪......),最后一个元素是什么,当前元素是什么。您永远不会存储整个元素,只是其中的一小部分(且大小不变)。 (但元数据确实需要 O(h) 空间)。
【解决方案2】:

根据 BST,这可以在 O(1) 中完成。

例如,如果树的节点有一个指向其父节点的指针,您可以使用 Wikipedia 页面的 nonRecrusiveInOrderTraversal 部分中描述的实现 Tree_traversal

作为另一种可能性,如果 BST 存储为一维数组,而不是节点之间的指针,您可以简单地遍历数组(并进行数学运算以确定每个节点的父节点和子节点)。

在进行遍历/遍历时,检查当前元素是否在正确的位置。

如果不是,那么您只需查看它应该在树中的哪个位置,并与该位置的元素进行交换。 (如果根在错误的位置,请注意)。

【讨论】:

    【解决方案3】:

    我们可以修改下面的 isBST() 方法来交换这两个随机交换的节点并更正它。

    /* Returns true if the given tree is a binary search tree
     (efficient version). */
    int isBST(struct node* node)
    {
      struct node *x = NULL;
      return(isBSTUtil(node, INT_MIN, INT_MAX,&x));
    }
    
    /* Returns true if the given tree is a BST and its
       values are >= min and <= max. */
    int isBSTUtil(struct node* node, int min, int max, struct node **x)
    {
    
      /* an empty tree is BST */
      if (node==NULL)
         return 1;
    
      /* false if this node violates the min/max constraint */
      if (node->data < min || node->data > max) {
         if (*x == NULL) {
            *x = node;//same the first incident where BST property is not followed.
         }
         else {
            //here we second node, so swap it with *x.
            int tmp = node->data;
            node->data = (*x)->data;
            (*x)->data = tmp;
    
         }
         //return 0;
         return 1;//have to return 1; as we are not checking here if tree is BST, as we know it is not BST, and we are correcting it.
      }
      /* otherwise check the subtrees recursively,
       tightening the min or max constraint */
      return
        isBSTUtil(node->left, min, node->data-1, x) &&  // Allow only distinct values
        isBSTUtil(node->right, node->data+1, max, x);  // Allow only distinct values
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多