コード例 #1
0
 def test_is_leaf(self):
     # Create node with no children
     node = BinaryTreeNode(2)
     assert node.is_leaf() is True
     # Attach left child node
     node.left = BinaryTreeNode(1)
     assert node.is_leaf() is False
     # Attach right child node
     node.right = BinaryTreeNode(3)
     assert node.is_leaf() is False
     # Detach left child node
     node.left = None
     assert node.is_leaf() is False
     # Detach right child node
     node.right = None
     assert node.is_leaf() is True
コード例 #2
0
def better_is_mirror(root: binary_tree.BinaryTreeNode):
    if root.is_leaf():
        return True

    if root.left is None or root.right is None:
        return False
        
    left_tree_stack = [root.left]
    right_tree_stack = [root.right]
    
    while len(left_tree_stack) != 0 or len(right_tree_stack) != 0:
        left_node = left_tree_stack.pop()
        right_node = right_tree_stack.pop()
        
        if not compare_node(left_node, right_node):
            return False
        
        dfs_left_tree(left_node, left_tree_stack.append)
        dfs_right_tree(left_node, right_tree_stack.append)
        
    return True