from typing import Set, Optional class Tree: def __init__( self ) -> None: self.left : Optional[ Tree ] = None self.right : Optional[ Tree ] = None def is_tree_rec( root: Tree, visited: Set[ Tree ] ) -> bool: if root in visited: return False visited.add( root ) result = True if root.left is not None: result = result and is_tree_rec( root.left, visited ) if root.right is not None: result = result and is_tree_rec( root.right, visited ) return result def is_tree( root: Tree ) -> bool: return is_tree_rec( root, set() ) import run_tests