在二叉搜索树中找到第二小的元素

finding second smallest element in binary search tree

int secondSmallestInBST(struct node * tNode) {
if( tNode==NULL || (tNode->left==NULL && tNode->right==NULL) )  // case 1 and 2
    exit;
if(tNode->left == NULL){                     // case 3
    tNode=tNode->right;
    while(tNode->left!=NULL){
        tNode=tNode->left;
    }
    return tNode->data;
}                                                     // general case.
node * parent=tNode,* child = tNode->left;
while(child->left!=NULL){
    parent = child;
    child = child->left;
}
return parent->data;

}

并不是每个测试用例都通过了我的代码。如果我的代码中缺少任何测试用例,请建议我。我只是在二叉搜索树中找到第二小的元素。

int secondSmallestInBST(struct node * tNode) {
if( tNode==NULL || (tNode->left==NULL && tNode->right==NULL) )  // case 1 and 2
    exit;
if(tNode->left == NULL){                     // case 3
    tNode=tNode->right;                     // find smallest in right bst.
    while(tNode->left!=NULL){
        tNode=tNode->left;
    }
    return tNode->data;
}                                                     // general case.
if(tNode->left->left==NULL && tNode->left->right!=NULL){   //missed case.
    tNode=tNode->left->right;
    while(tNode->left!=NULL){
        tNode=tNode->left;
    }
    return tNode->data;
}
node * parent= tNode;
node * child = tNode->left;
while(child->left!=NULL){
    parent = child;
    child = child->left;
}
return parent->data;

}

//这段代码中仍然缺少一些测试用例。

测试此案例 - 3 6 2 3。 树将如下所示:

    6
   /
  2
   \
    3

按照你的做法,答案会是 6,而实际是 3。

`

int Successor(Node* root){
  while(root->left){
    root = root->left;
  }
  return root->data;
}

int Second_Minimum(Node* root){
  //  make sure tree is not empty
  if(!root)
    return -1;
  // previous node takes before the last left node
  Node* previous = root;
  // check left node first for smallest key
  if(root->left){
    while(root->left){
      previous = root;
      root = root->left;        //    6
    }                           //   /
    // checks for the case ---->    2
    if(!root->right)            //   \
      return previous->data;    //    3 
  }
  // Go for minimum successor if exists 
  if(root->right)
    return Successor(root->right);
  // checked left and right branch root is on his own 
  return -1;
}

`

BST 中序遍历按顺序(排序)给出元素。所以想法是 return 遍历中的第二个元素(如果树少于两个元素,那么它不会有第二个最小值并且应该 return null(未找到))。

以下代码实现了该算法。请注意,该算法也可以轻松更改为 return 第 K 个最小元素。

代码是用 C# 编写的(很容易用其他语言编写:-)享受吧!

public static int? FindSecondMimimum(Node node)
{
    int current = 0;
    return FindKthMinimum(node, 2, ref current);
}

private static int? FindKthMinimum(Node node, int k, ref int current)
{
    int? result = null;
    if (node == null)
        return null;

    if (node.Left != null)
    {
        result = FindKthMinimum(node.Left, k, ref current);
        if (result != null)
            return result;
    }

    current++;
    if (current == k)
        return node.Value;

    if (node.Right != null)
    {
        result = FindKthMinimum(node.Right, k, ref current);
    }

    return result;
}