235. Lowest Common Ancestor of a Binary Search Tree

I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
Search for a command to run...

I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
No comments yet. Be the first to comment.
Data Structure & Algorithms : the most important and fundamental of any programmer. We will explore famous questions on datastructure & algorithms from various popular websites.
Let's discuss today the problem 378. Kth Smallest Element in a Sorted Matrix at leetcode. Problem link https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ The example in question has following Input: matrix = [ [1,5,9] [10,11,13] ...
Spring AI Tool Calling: From Chatbot to AI Agent with @Tool Your AI is smart. It knows an enormous amount. But it's frozen. It doesn't know what time it is right now. It doesn't know what's on your ca
Spring AI Advisors API Explained Series: Spring AI Complete Course — Lecture 4 of 12Reading Time: 8 minutesLevel: Intermediate Most developers stop at ChatClient. That is enough for demos. It is not
Working with Multiple AI Models in Spring AI Spring AI Complete Course — Lecture 3 of 12Previous: Lecture 2 — ChatClient API | Next: Lecture 4 — Advisors API In production AI applications, you rarel
Spring AI ChatClient API: The Fluent Heart of AI Integration Introduction If you've ever tried integrating AI models into a Java application, you know the pain. HTTP clients, API keys scattered everywhere, vendor-specific SDKs that never quite fit. W...
What is Spring AI? — Why Java Developers Need This in 2026 Every AI tutorial you see is in Python. LangChain, LlamaIndex, OpenAI SDK — all Python. But here's the uncomfortable truth: 80% of enterprise backends run Java. So who's building AI into thos...
Let's discuss problem no 235 at leetcode i.e 235. Lowest Common Ancestor of a Binary Search Tree. Problem Link: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
Here we are supposed to find the lowest common ancestor in a binary search tree. The lowest common ancestor is the node which is the first common ancestor of any given node.
If we look closely there are the following cases.
Let's check the code for it.
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root.val>p.val && root.val>q.val)
return lowestCommonAncestor( root.left , p, q);
if(root.val<p.val && root.val<q.val)
return lowestCommonAncestor( root.right , p, q);
return root;
}
}