{"record":{"id":"976ad9ae83ed3013","repo":"donnemartin/interactive-coding-challenges","slug":"root-cannot-be-none-976ad9","errorCode":null,"errorMessage":"root cannot be None","messagePattern":"root cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/check_balance/check_balance_solution.ipynb","lineNumber":119,"sourceCode":"    \"class BstBalance(Bst):\\n\",\n    \"\\n\",\n    \"    def _check_balance(self, node):\\n\",\n    \"        if node is None:\\n\",\n    \"            return 0\\n\",\n    \"        left_height = self._check_balance(node.left)\\n\",\n    \"        if left_height == -1:\\n\",\n    \"            return -1\\n\",\n    \"        right_height = self._check_balance(node.right)\\n\",\n    \"        if right_height == -1:\\n\",\n    \"            return -1\\n\",\n    \"        diff = abs(left_height - right_height)\\n\",\n    \"        if diff > 1:\\n\",\n    \"            return -1\\n\",\n    \"        return 1 + max(left_height, right_height)\\n\",\n    \"\\n\",\n    \"    def check_balance(self):\\n\",\n    \"        if self.root is None:\\n\",\n    \"            raise TypeError('root cannot be None')\\n\",\n    \"        height = self._check_balance(self.root)\\n\",\n    \"        return height != -1\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 3,\n   \"metadata\": {},\n   \"outputs\": [\n    {\n     \"name\": \"stdout\",","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/check_balance/check_balance_solution.ipynb#L101-L137","documentation":"Raised by BinaryTree.check_balance() in the check_balance_solution notebook when the tree's root is None. The method is written to treat a missing root as a programming/input error rather than an empty (and trivially balanced) tree, so it refuses to operate. It uses TypeError because the failure is an invalid state passed into the algorithm.","triggerScenarios":"Calling check_balance() on a newly constructed BinaryTree (root defaults to None), or on a tree whose root was never inserted/set. Any test that builds the tree but calls check_balance() before inserting nodes hits this immediately.","commonSituations":"Unit tests that instantiate BinaryTree() and assert check_balance() without seeding a root; refactors that leave a root-assignment line out; copying the solution class into a project where empty trees are expected to be valid input.","solutions":["Insert at least one node (e.g., tree.insert(1)) before calling check_balance()","If an empty tree is valid in your domain, catch TypeError and treat it as True (an empty tree is balanced)","Modify the class to return True for root is None instead of raising, if you own the code"],"exampleFix":"# before\ntree = BinaryTree()\ntree.check_balance()  # TypeError: root cannot be None\n\n# after\ntree = BinaryTree()\ntree.insert(1)\ntree.check_balance()  # False/True, no error","handlingStrategy":"validation","validationCode":"if tree.root is None:\n    print('empty tree; nothing to balance-check')\nelse:\n    result = tree.check_balance()","typeGuard":"def has_root(tree):\n    return tree is not None and getattr(tree, 'root', None) is not None","tryCatchPattern":"try:\n    tree.check_balance()\nexcept TypeError as e:\n    if 'root cannot be None' in str(e):\n        result = True  # empty tree is balanced by convention\n    else:\n        raise","preventionTips":["Always insert a root node before calling tree algorithms","Treat empty trees as a valid input case in your own wrappers"],"tags":["python","binary-tree","none-check","validation"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}