{"record":{"id":"af7b1b503aec7506","repo":"donnemartin/interactive-coding-challenges","slug":"root-cannot-be-none-af7b1b","errorCode":null,"errorMessage":"root cannot be None","messagePattern":"root cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/tree_lca/tree_lca_solution.ipynb","lineNumber":168,"sourceCode":"  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 3,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class LcaResult(object):\\n\",\n    \"\\n\",\n    \"    def __init__(self, node, is_ancestor):\\n\",\n    \"        self.node = node\\n\",\n    \"        self.is_ancestor = is_ancestor\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class BinaryTreeOptimized(object):\\n\",\n    \"\\n\",\n    \"    def lca(self, root, node1, node2):\\n\",\n    \"        if root is None:\\n\",\n    \"            raise TypeError('root cannot be None')\\n\",\n    \"        result = self._lca(root, node1, node2)\\n\",\n    \"        if result.is_ancestor:\\n\",\n    \"            return result.node\\n\",\n    \"        return None\\n\",\n    \"\\n\",\n    \"    def _lca(self, curr_node, node1, node2):\\n\",\n    \"        if curr_node is None:\\n\",\n    \"            return LcaResult(None, is_ancestor=False)\\n\",\n    \"        if curr_node is node1 and curr_node is node2:\\n\",\n    \"            return LcaResult(curr_node, is_ancestor=True)\\n\",\n    \"        left_result = self._lca(curr_node.left, node1, node2)\\n\",\n    \"        if left_result.is_ancestor:\\n\",\n    \"            return left_result\\n\",\n    \"        right_result = self._lca(curr_node.right, node1, node2)\\n\",\n    \"        if right_result.is_ancestor:\\n\",\n    \"            return right_result\\n\",\n    \"        if left_result.node is not None and right_result.node is not None:\\n\",\n    \"            return LcaResult(curr_node, is_ancestor=True)\\n\",","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/tree_lca/tree_lca_solution.ipynb#L150-L186","documentation":"BinaryTreeOptimized.lca raises TypeError('root cannot be None') when the root argument is None; the algorithm needs a non-empty tree to compute the lowest common ancestor. This differs from sibling implementations that return None, so read the signature carefully: root is a parameter here, not an attribute.","triggerScenarios":"Calling lca(None, node1, node2), or passing a tree's root that was never assigned (empty tree built from empty input).","commonSituations":"Constructing test trees via a helper that returns None for empty arrays, or switching from another LCA implementation in this repo that silently handles None roots, creating mismatched expectations.","solutions":["Ensure root is a real Node before calling lca (build the tree from non-empty data)","Guard: if root is not None: result = tree.lca(root, n1, n2)","Catch TypeError in tests covering the empty-tree edge case"],"exampleFix":"// before\nresult = solution.lca(tree_root, node1, node2)  # TypeError if tree_root is None\n// after\nif tree_root is not None:\n    result = solution.lca(tree_root, node1, node2)\nelse:\n    result = None","handlingStrategy":"type-guard","validationCode":"if root is None:\n    return None\nreturn solution.lca(root, node1, node2)","typeGuard":"def is_node(x) -> bool:\n    return x is not None and hasattr(x, 'left') and hasattr(x, 'right')","tryCatchPattern":"try:\n    return solution.lca(root, node1, node2)\nexcept TypeError:\n    return None  # empty tree has no LCA","preventionTips":["Never assume this implementation tolerates None roots like sibling versions","Validate tree-builder output before passing roots to LCA"],"tags":["tree","lca","ancestors","none-guard","python"],"backgroundTag":"none-argument-guard","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}