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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:25:38+00:00 2026-06-09T18:25:38+00:00

What is the easiest way to implement lowest common ancestor in Python? I have

  • 0

What is the easiest way to implement lowest common ancestor in Python? I have a tree which is represented by each node having a pointer to its parent, and I want to be able to find the first common ancestor given two nodes. I’ve come up with several ideas but none are particularly appealing

  1. Have each node contain a list of its bases, and to perform a join, find the longest common prefix and then take the last element. Unfortunately, I don’t know of any builtin way to do longest common prefix so this requires manual looping.

  2. Have each node contain a set of its bases and perform a set intersection, and take the maximum element. But this requires defining custom comparison operators, and I’m not even sure if it would work.

What should I do? I’m looking for something that favors simplicity over performance, so solutions requiring complicated processing are out.

Edit: I discovered that while there isn’t a builtin way, you can do longest common prefix in one line using zip, so it’s still fairly simple.

common = [x for x in zip(*baselists) if len(set(x)) == 1][-1]
  • 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-09T18:25:40+00:00Added an answer on June 9, 2026 at 6:25 pm

    Under the assumption that you cannot amend your tree to include depth, you can do the following:

    For each Node, recursively traverse the tree upwards until you hit the root. At each parent node, insert the node into a list. This should give you list_a and list_b. Iterate over the shortest list, comparing elements from each list. When you find one that doesn’t match, the previous entry is your largest parent element.

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

Sidebar

Related Questions

hi what is the easiest way to implement asynch operations on WPF and MVVM,
Which is the easiest way to validate a file and to prevend changing it's
What is the easiest way to do the equivalent of rm -rf in Python?
What is the easiest way to implement watermark textbox control in ASP.NET MVC, are
What's the easiest way to implement lan interprocess communication? I need process on machine
What is the easiest way to implement HTTP push (comet) on Django? Are there
I need to know the easiest way to implement asp.net 3.5 URL routing as
Using DevExpress's Grid, what the easiest way to implement a delete of a selection
Whats the easiest way to implement a lookup table that checks to see if
What is the easiest way to implement a UserControl that mimics a hot track

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.