Lowest Common Ancestor Of Binary Tree In Python | letsbug
Hey in the article we are looking at a coding challenge problem. You might have come across this it is called finding the lowest common ancestor in a binary tree. So we will find the solution to this problem in python. Let's start. The answer is simple we have to visit every node and check if its child is same as it is asked if so then you return the node and we repeat this process for subtrees of the tree and then if left and right node exit we return it. to the top. code: class Node : def __init__ (self,data): self . data = data self . left = None self . right = None class binaryTree : def __init__ (self): self . root = None #inserts a node in binary tree def insert (self,data): if ( self . root == None ): ...