Skip to content

Bug Report for lowest-common-ancestor-in-binary-search-tree #5514

@sandeepilania

Description

@sandeepilania

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

# 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':
        def dfs(node):
            if not node:
                return None

            # Compare by node identity (preferred for this problem)
            if node is p or node is q:
                return node

            left = dfs(node.left)
            right = dfs(node.right)

            if left and right:
                return node

            return left or right

        return dfs(root)

    this solution is good i think. I tested on Leetcode as well and it works fine.

But here i am getting error:
Traceback (most recent call last):
File "/box/main.py", line 90, in
raise TypeError(f"Your output was {outputNode}, but the expected return type is TreeNode")
TypeError: Your output was None, but the expected return type is TreeNode

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