{"record":{"id":"70c62d4cd9435677","repo":"donnemartin/interactive-coding-challenges","slug":"root-cannot-be-none-70c62d","errorCode":null,"errorMessage":"root cannot be None","messagePattern":"root cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/invert_tree/invert_tree_solution.ipynb","lineNumber":114,"sourceCode":"   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"%run ../bst/bst.py\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class InverseBst(Bst):\\n\",\n    \"\\n\",\n    \"    def invert_tree(self):\\n\",\n    \"        if self.root is None:\\n\",\n    \"            raise TypeError('root cannot be None')\\n\",\n    \"        return self._invert_tree(self.root)\\n\",\n    \"\\n\",\n    \"    def _invert_tree(self, root):\\n\",\n    \"        if root is None:\\n\",\n    \"            return\\n\",\n    \"        self._invert_tree(root.left)\\n\",\n    \"        self._invert_tree(root.right)\\n\",\n    \"        root.left, root.right = root.right, root.left\\n\",\n    \"        return root\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/invert_tree/invert_tree_solution.ipynb#L96-L132","documentation":"InverseBst.invert_tree raises TypeError('root cannot be None') when called on an empty BST. The class delegates to _invert_tree starting from self.root, and the guard clause treats an empty tree as an invalid argument rather than a no-op, so callers must ensure the tree has at least one node before inverting.","triggerScenarios":"Calling invert_tree() on a freshly constructed InverseBst (or any Bst subclass) without inserting any nodes first, e.g. tree = InverseBst(); tree.invert_tree().","commonSituations":"Running the solution on an empty test case, initializing a BST from an empty array/list, or a test harness that iterates over edge cases including [] before populated trees.","solutions":["Insert at least one node (e.g. tree.insert(5)) before calling invert_tree()","Guard the call: if tree.root is not None: tree.invert_tree()","Catch TypeError in test code when intentionally exercising the empty-tree case"],"exampleFix":"// before\ntree = InverseBst()\ntree.invert_tree()  # TypeError\n// after\ntree = InverseBst()\nif tree.root is not None:\n    tree.invert_tree()","handlingStrategy":"type-guard","validationCode":"assert tree.root is not None, 'cannot invert an empty tree'","typeGuard":"def can_invert(tree) -> bool:\n    return tree.root is not None","tryCatchPattern":"try:\n    tree.invert_tree()\nexcept TypeError:\n    pass  # empty tree, nothing to invert","preventionTips":["Always insert at least one node before calling tree operations","Wrap notebook demos with a root check to handle empty-tree edge cases"],"tags":["binary-tree","bst","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"}