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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:16:16+00:00 2026-05-15T18:16:16+00:00

A binary tree of N nodes is ‘curious’ if it is a binary tree

  • 0

A binary tree of N nodes is ‘curious’ if it is a binary tree whose node values are 1, 2, ..,N and which satisfy the property that

  • Each internal node of the tree has exactly one descendant which is greater than it.
  • Every number in 1,2, …, N appears in the tree exactly once.

Example of a curious binary tree

  4
 / \
5   2
   / \
  1   3

Can you give an algorithm to generate a uniformly random curious binary tree of n nodes, which runs in O(n) guaranteed time?

Assume you only have access to a random number generator which can give you a (uniformly distributed) random number in the range [1, k] for any 1 <= k <= n. Assume the generator runs in O(1).

I would like to see an O(nlogn) time solution too.

Please follow the usual definition of labelled binary trees being distinct, to consider distinct curious binary trees.

  • 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-15T18:16:17+00:00Added an answer on May 15, 2026 at 6:16 pm

    Aha, I think I’ve got how to create a random heap in O(N) time. (after which, use approach in Greg Kuperberg’s answer to transform into “curious” binary tree.)

    edit 2: Rough pseudocode for making a random min-heap directly. Max-heap is identical except the values inserted into the heap are in reverse numerical order.

    struct Node {
       Node left, right;
       Object key;
       constructor newNode() { 
         N = new Node; 
         N.left = N.right = null; 
         N.key = null;
       }
    }
    
    function create-random-heap(RandomNumberGenerator rng, int N)
    {
       Node heap = Node.newNode();
       // Creates a heap with an "incomplete" node containing a null, and having
       // both child nodes as null.
    
       List incompleteHeapNodes = [heap];
       // use a vector/array type list to keep track of incomplete heap nodes.
    
       for k = 1:N
       {
          // loop invariant: incompleteHeapNodes has k members. Order is unimportant.
    
         int m = rng.getRandomNumber(k);
         // create a random number between 0 and k-1
         Node node = incompleteHeapNodes.get(m);
         // pick a random node from the incomplete list, 
         // make it a complete node with key k.
         // It is ok to do so since all of its parent nodes
         // have values less than k.
         node.left = Node.newNode();
         node.right = Node.newNode();
         node.key = k;
    
         // Now remove this node from incompleteHeapNodes
         // and add its children. (replace node with node.left,
         // append node.right)
    
         incompleteHeapNodes.set(m, node.left);
         incompleteHeapNodes.append(node.right);
    
         // All operations in this loop take O(1) time.
       }
    
       return prune-null-nodes(heap);
    }
    
    // get rid of all the incomplete nodes.
    function prune-null-nodes(heap)
    {
       if (heap == null || heap.key == null)
          return null;
       heap.left = prune-null-nodes(heap.left);
       heap.right = prune-null-nodes(heap.right);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 453k
  • Answers 453k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here’s an HTML workaround effective in IE and Google Chrome.… May 15, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer This is part of both standard C and C++ and… May 15, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer http://dailybooth.com/rss/username/pictures.rss ? May 15, 2026 at 9:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.