Skip to content

Bug Report for lowest-common-ancestor-of-a-binary-tree #4717

@TyronSamaroo

Description

@TyronSamaroo

Bug Report for https://neetcode.io/problems/lowest-common-ancestor-of-a-binary-tree

Saying Solution below is wrong??

Definition for a binary tree node.

class TreeNode:

def init(self, x):

self.val = x

self.left = None

self.right = None

class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
cur = root

    #a split or we reach the node thats common to itself 

    while cur:
        if p.val > cur.val and q.val > cur.val:
            cur = cur.right
        elif p.val < cur.val and q.val < cur.val:
            cur = cur.left
        else:
            return cur

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions