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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:37:12+00:00 2026-06-13T16:37:12+00:00

I got this question in a programming interview. Feel free to think about how

  • 0

I got this question in a programming interview. Feel free to think about how you might answer it.

You’re given the root node of a binary tree (not a binary search tree) where each node contains an integer value, and no value appears twice. You’re also given two values val1 and val2 (which may or may not be in the tree.) If both are in the tree, return the node that is the least common ancestor of the two nodes containing these two values. If not, return null.

Assume that each node has access to the left and right children. You may append the node structure, but you may not append the parent to each node. Your algorithm should run in less than O(N^2), where N is the number of nodes in the tree.

NOTE: Although it’s similar to the famous least-common-ancestor problem, the limitations in this one makes it not quite the same.

  • 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-13T16:37:13+00:00Added an answer on June 13, 2026 at 4:37 pm

    I don’t understand why you would consider O(N2), unless I’m misunderstanding the problem. The following solution is a preorder traverse and therefore visits each node at most once:

    struct node* visit(struct node* visited, int p, int q, struct node* sentinel) {
      if (!visited) return visited;
      if (visited->data == p || visited->data == q) {
        struct node* t;
        if ((t = visit(visited->left, p, q, visited))) return t;
        if ((t = visit(visited->right, p, q, visited))) return t;
        return sentinel;
      } else {
        struct node* left = visit(visited->left, p, q, visited);
        struct node* right = visit(visited->right, p, q, visited);
        if (left == visited) return right ? right : sentinel;
        if (right == visited) return left ? left : sentinel;
        return left ? left : right;
      }
    }
    
    struct node* lca(struct node* root, int val1, int val2) {
      return visit(root, val1, val2, 0);
    }
    

    I suppose some explanation is in order, but maybe since it was just a brain teaser, it’s best to leave that code there and see what people make of it. (And I haven’t tested it thoroughly either, to make it look more like an interview.)

    This is not a question I’ve ever used in an interview, and I’m not sure what I would have made had someone come up with the above code as an answer. But then I’ve never been sure that I would have hired myself.

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

Sidebar

Related Questions

Possible Duplicate: LRU cache design I got this question in a programming interview. Feel
I got this question in a programming test. Do you think this question is
I got this in an interview question -- the question was more about what
Today I got this question for which I think I answered very bad. I
I just got this question on an interview and had no idea how to
This question got me thinking about bare strings. When PHP sees a string that's
This question got me thinking about the max_size method in vector class. It is
NOTICE: this question has a ambiguous concept about 'degree'. and I have got my
Relatively new to Cocoa here. This question is about NSFileHandle, but I got a
Disclaimer: This is not actually a programming question, but I feel the audience on

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.