Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:33:32+00:00 2026-05-25T21:33:32+00:00

I know how to find the max depth of a tree using a stack

  • 0

I know how to find the max depth of a tree using a stack and inorder traversal, but I cannot figure out how to find the min depth of a tree (not necessarily a BST) using a stack or queue instead of recursive calls.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:33:33+00:00Added an answer on May 25, 2026 at 9:33 pm

    One thing to note here is that when you perform recursion you are using your process execution stack. This generally has some limit set by OS. So with each recursion the process state is pushed onto this stack. So at some point stackoverflow occurs.

    If you end up doing a iterative version as apposed to recursive, note that the difference here is that this stack implementation is maintained by you. There is lot more work involved but stackoverflow is averted…

    We could do something like the following (recursive version)-

    MIN-VALUE

    int min = INT_MAX;
    void getMin(struct node* node)
    {
         if (node == NULL)
              return;
    
         if(node->data < min)
              min = node->data;
    
         getMin(node->left);
         getMin(node->right);
    
         return min;
    }
    

    Alternatively you could use min-heap which gives you minimum value in constant time.

    UPDATE: Since you changed your question to min-depth

    MIN-DEPTH

    #define min(a, b) (a) < (b) ? (a) : (b)
    
    typedef struct Node
    {
        int data;
        struct Node *left, *right;
    
    }Node;
    
    typedef Node * Tree;
    
    int mindepth(Tree t)
    {
        if(t == NULL || t->left == NULL || t->right == NULL)
            return 0;
    
        return min( 1 + mindepth(t->left), 1 +  mindepth(t->right) );
    }
    

    PS: Code is typed freehand, there might be syntactical errors but I believe logic is fine…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can find out if a variable is null in Java using
The only way I know is: find /home -xdev -samefile file1 But it's really
I know how to find a method in java using a fixed string, someClass.getMethod(foobar,
I know how to find what I need from XML using XPath. The syntax
Does anyone know how to find out the precision of long double on a
I have been looking for a while now but I can not find an
I am using MYSQL5.1,Though I tried to find documentation for this but was unsuccessful,secondly
Is there any easy way to find min/max value in the set of input
I know how to write the request I need in SQL but can't find
I want to find the fastest way to get the min and max of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.