-1

我正在二叉搜索树的有序遍历中搜索数据的位置(索引号)。

void inorder(struct node *root) {    
  if(!root)
      return NULL;
  inorder(root->left);
  cout<<root->data;
  inorder(root->right);
}

我如何修改此函数以获取给定数字的位置。

4

1 回答 1

0

您可以通过引用传递一个整数并在您的函数中增加它:

void inorder(struct node *root, int &position){    
    if(!root)
        return NULL;
    inorder(root->left, position);
    position++;
    cout<<position<<": "<<root->data;
    inorder(root->right, position);
}
于 2015-08-21T15:02:29.407 回答