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 7702561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:15:18+00:00 2026-05-31T23:15:18+00:00

When given a number of nodes we are able to calculate the min depth

  • 0

When given a number of nodes we are able to calculate the min depth of the binary tree by doing log2(n)

Where n is the number of nodes.

If you draw the tree out for the maximum depth for example 12 nodes you work out that the maximum depth can only be 4 if the tree is to remained balanced.

                0
               /   \
              0     0
            /  \   / \
           0    0 0   0
          /\     \     \ 
        0   0      0    0

Sorry for the bad ascii art. Does anyone know of a forumla that is able to calculate the max depth of a binary tree when given the number of nodes? Or at least point me in the right direction?

  • 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-31T23:15:19+00:00Added an answer on May 31, 2026 at 11:15 pm

    By using the root element :

    int maxHeight(BinaryTree *p) {
      if (!p) return 0;
      int left_height = maxHeight(p->left);
      int right_height = maxHeight(p->right);
      return (left_height > right_height) ? left_height + 1 : right_height + 1;
    }
    

    By using the number of nodes and some math logic (which I definitely cannot express properly (I’m by no means a math guru); but here it is) :

    Observation :

    • 2-3 nodes => maxDepth = 1 (2 = 2^1, 3 = 2^1,.. < 2^2 )
    • 4-7 nodes => maxDepth = 2 (4 = 2^2, 5 = 2^2,.., 6 = 2^2,.., 7 = 2^2,… < 2^3)
    • 8-15 nodes => maxDepth = 3
    • …

    Analysis :

    • m => max Depth (actual the INT part of the depth, discard any decimal places)
    • n => number of nodes

    • ln => natural logarithm (=log[e])

    • 2^m = n

    • ln(2^m) = ln(n)

    • m*ln(2) = ln(n)
    • m = ln(n)/ln(2)

    Conclusion :

    Now, if m = 2,… , then the maximum depth is 2. Just get the int part of it. 😉


    NOTE: I’m definitely re-inventing the wheel here; but that’s probably part of the fun of dealing with something you know nothing about; and doing it, solely following your instinct and observations… 🙂

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

Sidebar

Related Questions

Given number of binary tree nodes (X) write method that returns the number of
Can anyone tell me how to format a given number of seconds (for example
I'm using Perl to run through a tree, and then calculate the leaf nodes
Given this binary tree (actually, the binary tree can be random and dynamic, this
Hi I have the following problem. Given a Tree I want to be able
The task is to generate a given number of numeric pins of a given
How can I rotate an Bitmap a given number of degrees while maintaining the
How can I find the least prime number greater than a given number? For
How to write multithread windows application that runs a program in given number of
I'm writing a code to identify the subfix for a given number. I have

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.