Search code examples
c++binary-treebinary-search-treeminimum

maximum and minimum value of binary tree


I don't understand how this isn't working:

Node* BST::Minimum(Node *curr) {

    if (curr->left != NULL) {
        Minimum(curr->left);
    }

    return curr;

}

I mean I already did insert fine, and printpostorder, inorderr, preorder fine.

Am I doing something silly? This just prints the root node.


Solution

  • You call Minimum and throw its return value away. You want return Minimum(curr->left);.

    By the way, I strongly recommend doing this iteratively rather than recursively, like this:

      Node* node = curr;
      while (node->left != NULL)
          node = node->left;
      return node;