def test_nontrivial(): # 3 # / \ # 5 1 # / \ \ # 6 2 8 # / \ # 7 4 root = TreeNode(3) target = TreeNode(5) root.left = target target.left = TreeNode(6) target.right = TreeNode(2, TreeNode(7), TreeNode(4)) root.right = TreeNode(1, None, TreeNode(8)) res = all_nodes_k_distance(root, target, 2) assert set([1, 4, 7]) == set(res)
def test_nontrivial(): # 4 4 # / \ / \ # 2 7 -> 7 2 # / \ / \ / \ / \ # 1 3 6 9 9 6 3 1 tree = TreeNode(4) tree.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) tree.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9)) tree_inv = TreeNode(4) tree_inv.left = TreeNode(7, left=TreeNode(9), right=TreeNode(6)) tree_inv.right = TreeNode(2, left=TreeNode(3), right=TreeNode(1)) assert is_same_tree(invert(tree), tree_inv)
def test_nontrivial_true(): # 4 4 # / \ / \ # 2 7 == 2 7 # / \ / \ / \ / \ # 1 3 6 9 1 3 6 9 tree1 = TreeNode(4) tree1.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) tree1.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9)) tree2 = TreeNode(4) tree2.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) tree2.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9)) assert is_same_tree(tree1, tree2)
def test_nontrivial_false(): # 4 4 # / \ / \ # 2 7 != 7 2 # / / \ / \ / \ # 1 6 9 9 6 3 1 tree1 = TreeNode(4) tree1.left = TreeNode(2, left=TreeNode(1)) tree1.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9)) tree2 = TreeNode(4) tree2.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) tree2.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9)) assert not is_same_tree(tree1, tree2)
def test_nontrivial_true(): # 4 # / \ # 2 is subtree of 2 7 # / \ / \ / \ # 1 3 1 3 6 9 tree1 = TreeNode(2) tree1.left = TreeNode(1) tree1.right = TreeNode(3) tree2 = TreeNode(4) tree2.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) tree2.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9)) assert is_subtree(tree1, tree2)
def _array_to_bst(L, first, last): if first > last: return None mid = (first + last) // 2 node = TreeNode(L[mid]) node.left = _array_to_bst(L, first, mid - 1) node.right = _array_to_bst(L, mid + 1, last) return node
def test_nontrivial_chain(): # 4 # / # 2 # / # 1 root = TreeNode(4) root.left = TreeNode(2, left=TreeNode(1)) assert get_min_height(root) == 3
def test_preorder(): # 4 # / \ # 2 5 # / \ # 1 3 root = TreeNode(4) root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) root.right = TreeNode(5) assert preorder(root) == [4, 2, 1, 3, 5]
def test_nontrivial_false(): # 3 # / \ # 2 5 # / \ # 1 4 root = TreeNode(3) root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(4)) root.right = TreeNode(5) assert not is_bst(root)
def test_nontrivial_true(): # 4 # / \ # 2 5 # / \ # 1 3 root = TreeNode(4) root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) root.right = TreeNode(5) assert is_bst(root)
def test_nontrivial(): # 4 # / \ # 2 5 # / \ # 1 3 root = TreeNode(4) root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) root.right = TreeNode(5) assert get_path(root, 3) == [4, 2, 3]
def test_inorder(): # 4 # / \ # 2 5 # / \ # 1 3 root = TreeNode(4) root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) root.right = TreeNode(5) assert inorder(root) == [1, 2, 3, 4, 5]
def test_postorder(): # 4 # / \ # 2 5 # / \ # 1 3 root = TreeNode(4) root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) root.right = TreeNode(5) assert postorder(root) == [1, 3, 2, 5, 4]
def test_nontrivial(): # 4 # / \ # 2 5 # / \ # 1 3 root = TreeNode(4) root.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) root.right = TreeNode(5) assert get_height(root) == 3
def test_nontrivial(): # 4 # / \ # 2 5 # / \ # 1 3 root = TreeNode(4) root.left = TreeNode(2, left=TreeNode(1)) root.right = TreeNode(5, right=TreeNode(3)) traversal = level_order_traversal(root) assert traversal == [4, 2, 5, 1, 3]
def test_nontrivial(): # 5 # / \ # 3 8 # / \ # 2 4 tree = TreeNode(4) tree.left = TreeNode(3, left=TreeNode(2), right=TreeNode(4)) tree.right = TreeNode(8) assert find_min(tree) == 2
def test_nontrivial_false(): # 5 # / \ # 3 8 # / \ # 2 4 tree = TreeNode(4) tree.left = TreeNode(3, left=TreeNode(2), right=TreeNode(4)) tree.right = TreeNode(8) assert not search(tree, 1)
def test_nontrivial_true(): # 6 # / \ # 2 8 # / \ / \ # 1 3 7 9 L = [1, 2, 3, 6, 7, 8, 9] tree = array_to_bst(L) expected = TreeNode(6) expected.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) expected.right = TreeNode(8, left=TreeNode(7), right=TreeNode(9)) assert is_same_tree(tree, expected)
def test_nontrivial(): # 5 # / \ # 3 8 # / \ # 2 4 tree = TreeNode(5) tree.left = TreeNode(3, left=TreeNode(2), right=TreeNode(4)) tree.right = TreeNode(8) lca = get_lca(tree, 2, 4) assert lca.val == 3
def test_nontrivial(): # 4 # / \ # 2 7 # / \ / \ # 1 3 6 9 tree = TreeNode(4) tree.left = TreeNode(2, left=TreeNode(1), right=TreeNode(3)) tree.right = TreeNode(7, left=TreeNode(6), right=TreeNode(9)) lca = get_lca(tree, 1, 3) assert lca.val == 2
def test_trivial(): root = TreeNode(0) left = TreeNode(1) root.left = left res = all_nodes_k_distance(root, left, 1) assert res == [0]