236. Lowest Common Ancestor of a Binary Tree

class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
return self.rec(root,p,q)

def rec(self,root,p,q):
if root ==None or (root==p) or (root==q):
return root
left =self.rec(root.left,p,q)
right = self.rec(root.right,p,q)

if left ==None:
return right
elif right ==None:
return left
else:
return root

--

--

--

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to debug a Kubernetes service deployment

Some time ago, I looked at using the Service Monitoring API to create basic SLOs against “out of…

Guide to set-up an Ad blocker & VPN Server on Cloud

Java 12 Features

Triathon NFT Auction Mechanism Encourages More Participation in Auctions

Building Real-time Apps using WebSockets

Making SQL Fun Again with Azure Data Studio

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

More from Medium

“ Experience creating a high level architecture and how it helps to see an overall picture of the…

CS373 Spring 2022: Nathan Whyte

[Shared] Some tips to make your code cleaner

CAUSES AND CONSEQUENCES OF THE ARAB SPRING In Syria