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

  • Home
  • SEARCH
  • 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 9149097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:25:51+00:00 2026-06-17T11:25:51+00:00

Suppose that you are given an arbitrary binary tree. We’ll call the tree balanced

  • 0

Suppose that you are given an arbitrary binary tree. We’ll call the tree balanced if the following is true for all nodes:

  1. That node is a leaf, or
  2. The height of the left subtree and the height of the right subtree differ by at most ±1 and the left and right subtrees are themselves balanced.

Is there an efficient algorithm for determining the minimum number of nodes that need to be added to the tree in order to make it balanced? For simplicity, we’ll assume that nodes can only be inserted as leaf nodes (like the way that a node is inserted into a binary search tree that does no rebalancing).

  • 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-06-17T11:25:52+00:00Added an answer on June 17, 2026 at 11:25 am

    The following tree fits into your definition, although it doesn’t seem very balanced to me:

    Depth 5 "balanced" tree

    EDIT This answer is wrong, but it has enough interesting stuff in it that I don’t feel like deleting it yet. The algorithm produces a balanced tree, but not a minimal one. The number of nodes it adds is:

    where n ranges over all nodes in the tree, lower(n) is the depth of the child of n with the lower depth and upper(n) is the depth of the child of n with the higher depth. Using the fact that the sum of the first k fibonacci numbers is fib(k+2)-1, we can replace the inner sum with fib(upper(n)) - fib(lower(n) + 2).

    The formula is (more or less) derived from the following algorithm to add nodes to the tree, making it balanced (in python, only showing the relevant algorithms):

    def balance(tree, label):
      if tree is None:
        return (None, 0)
      left, left_height = balance(tree.left_child, label)
      right, right_height = balance(tree.right_child, label)
      while left_height < right_height - 1:
        left = Node(label(), left, balanced_tree(left_height - 1, label))
        left_height += 1
      while right_height < left_height - 1:
        right = Node(label(), right, balanced_tree(right_height - 1, label))
        right_height += 1
      return (Node(tree.label, left, right), max(left_height, right_height) + 1)
    
    def balanced_tree(depth, label):
      if depth <= 0:
        return None
      else:
        return Node(label(),
                    balanced_tree(depth - 1, label),
                    balanced_tree(depth - 2, label))
    

    As requested: report the count instead of creating the tree:

    def balance(tree):
      if tree is None:
        return (0, 0)
      left, left_height = balance(tree.left_child)
      right, right_height = balance(tree.right_child)
      while left_height < right_height - 1:
        left += balanced_tree(left_height - 1) + 1
        left_height += 1
      while right_height < left_height - 1:
        right += balanced_tree(right_height - 1) + 1
        right_height += 1
      return (left + right, max(left_height, right_height) + 1)
    
    def balanced_tree(depth):
      if depth <= 0:
        return 0
      else:
        return (1 + balanced_tree(depth - 1)
                  + balanced_tree(depth - 2))
    

    Edit: Actually, I think that other than computing the size of a minimum balanced tree of depth n more efficiently (i.e. memoizing it, or used the closed form: it’s just fibonacci(n+1)-1), that’s probably as efficient as you can get, since you have to examine every node in the tree in order to test the balance condition, and that algorithm looks at every node precisely once.

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

Sidebar

Related Questions

suppose that there is given following matlab code function dp = derp(p) n =
Suppose that my Haskell function is given an input, which is supposed to be
Let E be a given directed edge set. Suppose it is known that the
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
Here is the thing.I wanna traverse any arbitrary two nodes in my N-ary Tree(Family
Suppose that I have a unsigned char*, let's call it: some_data unsigned char* some_data;
Suppose that I've a given html code: <ul class=fmenu123> <li>city</li> <li>service</li> <li>hour</li> </ul> There
I'm stumped by the following homework question for an algorithms class: Suppose that we
Suppose that we are given an m x n grid of zeros and ones
Suppose that you're given an array A of n distinct elements drawn from some

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.