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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:58:27+00:00 2026-06-02T17:58:27+00:00

Check if 2 tree nodes are related (i.e. ancestor-descendant) solve it in O(1) time,

  • 0

Check if 2 tree nodes are related (i.e. ancestor-descendant)

  • solve it in O(1) time, with O(N) space (N = # of nodes)
  • pre-processing is allowed

That’s it. I’ll be going to my solution (approach) below. Please stop if you want to think yourself first.


For a pre-processing I decided to do a pre-order (recursively go through the root first, then children) and give a label to each node.

Let me explain the labels in details. Each label will consist of comma-separated natural numbers like “1,2,1,4,5” – the length of this sequence equals to (the depth of the node + 1). E.g. the label of the root is “1”, root’s children will have labels “1,1”, “1,2”, “1,3” etc.. Next-level nodes will have labels like “1,1,1”, “1,1,2”, …, “1,2,1”, “1,2,2”, …

Assume that “the order number” of a node is the “1-based index of this node” in the children list of its parent.

Common rule: node’s label consists of its parent label followed by comma and “the order number” of the node.

Thus, to answer if two nodes are related (i.e. ancestor-descendant) in O(1), I’ll be checking if the label of one of them is “a prefix” of the other’s label. Though I’m not sure if such labels can be considered to occupy O(N) space.

Any critics with fixes or an alternative approach is expected.

  • 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-02T17:58:28+00:00Added an answer on June 2, 2026 at 5:58 pm

    You can do it in O(n) preprocessing time, and O(n) space, with O(1) query time, if you store the preorder number and postorder number for each vertex and use this fact:

    For two given nodes x and y of a tree T, x is an ancestor of y if and
    only if x occurs before y in the preorder traversal of T and after y
    in the post-order traversal.

    (From this page: http://www.cs.arizona.edu/xiss/numbering.htm)

    What you did in the worst case is Theta(d) where d is the depth of the higher node, and so is not O(1). Space is also not O(n).

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

Sidebar

Related Questions

I rendered a check box node tree. The renderer renders the parent nodes with
I am currently working on some POC using JS Tree plugin and related check
i have a tree of nodes that i dont want some type of nodes
This is the feature i want to implement: check a tree node by click
I am struggling with a method of walking a directory tree to check existence
I want to make a function in standard ml that checks if a tree
I want to implement a tree with checkable root nodes and some parameters (actually,
My application will be looping thru the Virtual Nodes and check their data. I
I have been trying to implement a CheckBox Node Tree, where the parent nodes
High Guys, I have to define a polymorphic datatype for a tree that can

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.