Skip to content

Bug Report for binary-tree-diameter #6108

Description

@darce2

Bug Report for https://neetcode.io/problems/binary-tree-diameter

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

This solution returns correct but is actually incorrect. This test case proves it it is incorrect but neetcode allows the below submission as correct.
[1,2,3,4,5,null,null,6,7,8,9]

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        def dfs(node, count):
            if node.left and node.right:
                return dfs(node.left, 1) +  dfs(node.right, 1)

            elif node.left:
                return dfs(node.left, count + 1)

            elif node.right:
                return dfs(node.right, count + 1)

            return count

        return dfs(root, 0)

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