Пример #1
0
def test_generateTrees1():
    root = TreeNode.stringToTreeNode(
        "[5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1]")
    sum = 22
    expect = [[5, 8, 4, 5], [5, 4, 11, 2]]
    actual = solution.pathSum(root, sum)
    assert sorted(actual) == sorted(expect)
Пример #2
0
def test_iterator():
    root = TreeNode.stringToTreeNode("[7,3,15,null,null,9,20]")
    iterator = BSTIterator(root)
    assert iterator.next() == 3
    assert iterator.next() == 7
    assert iterator.hasNext()
    assert iterator.next() == 9
    assert iterator.hasNext()
    assert iterator.next() == 15
    assert iterator.hasNext()
    assert iterator.next() == 20
    assert not iterator.hasNext()
Пример #3
0
def test_generateTrees2():
    root = TreeNode.stringToTreeNode("[]")
    expect = []
    actual = solution.zigzagLevelOrder(root)
    assert actual == expect
Пример #4
0
def test_isValidBST1():
  root = TreeNode.stringToTreeNode("[2,1,3]")
  actual = solution.isValidBST(root)
  assert actual
Пример #5
0
def test_isValidBST5():
  root = TreeNode.stringToTreeNode("[10,5,15,null,null,6,20]")
  actual = solution.isValidBST(root)
  assert not actual
Пример #6
0
def test_sumNumbers2():
    root = TreeNode.stringToTreeNode('[4,9,0,5,1]')
    expect = 1026
    actual = solution.sumNumbers(root)
    assert actual == expect
Пример #7
0
def test_preorderTraversal1():
    root = TreeNode.stringToTreeNode("[1,null,2,3]")
    expect = [1, 2, 3]
    actual = solution.preorderTraversal(root)
    assert actual == expect
Пример #8
0
def test_buildTree2():
    preorder = []
    inorder = [9]
    expect = TreeNode.stringToTreeNode("[]")
    actual = solution.buildTree(preorder, inorder)
    assert TreeNode.isSame(actual, expect)
Пример #9
0
def test_buildTree1():
  head = ListNode.stringToListNode("[-10,-3,0,5,9]")
  expect = TreeNode.stringToTreeNode("[0,-3,9,-10,null,5]")
  actual = solution.sortedListToBST(head)
  assert TreeNode.isSame(actual, expect)
Пример #10
0
def test_buildTree1():
    root = TreeNode.stringToTreeNode("[1,2,5,3,4,null,6]")
    expect = TreeNode.stringToTreeNode(
        "[1,null,2,null,3,null,4,null,5,null,6]")
    solution.flatten(root)
    assert TreeNode.isSame(root, expect)
Пример #11
0
def test_buildTree2():
    root = TreeNode.stringToTreeNode("[]")
    expect = TreeNode.stringToTreeNode("[]")
    solution.flatten(root)
    assert TreeNode.isSame(root, expect)
Пример #12
0
def test_buildTree1():
  inorder = [9, 3, 15, 20, 7]
  postorder = [9, 15, 7, 20, 3]
  expect = TreeNode.stringToTreeNode("[3,9,20,null,null,15,7]")
  actual = solution.buildTree(inorder, postorder)
  assert TreeNode.isSame(actual, expect)
Пример #13
0
def test_sumNumbers1():
    root = TreeNode.stringToTreeNode('[1,2,3]')
    expect = 25
    actual = solution.sumNumbers(root)
    assert actual == expect
Пример #14
0
def test_sumNumbers4():
    root = TreeNode.stringToTreeNode('[1, null, 2]')
    expect = 12
    actual = solution.sumNumbers(root)
    assert actual == expect
Пример #15
0
def test_sumNumbers3():
    root = TreeNode.stringToTreeNode('[]')
    expect = 0
    actual = solution.sumNumbers(root)
    assert actual == expect
Пример #16
0
def test_generateTrees3():
    root = TreeNode.stringToTreeNode("[1,2,3,4,null,null,5]")
    expect = [[1], [3, 2], [4, 5]]
    actual = solution.zigzagLevelOrder(root)
    assert actual == expect
Пример #17
0
def test_generateTrees1():
    root = TreeNode.stringToTreeNode("[3,9,20,null,null,15,7]")
    expect = [[3], [20, 9], [15, 7]]
    actual = solution.zigzagLevelOrder(root)
    assert actual == expect
Пример #18
0
def test_inorderTraversal2():
  root = TreeNode.stringToTreeNode("[1,2,3]")
  expect = [2, 1, 3]
  actual = solution.inorderTraversal(root)
  assert actual == expect
Пример #19
0
def test_buildTree1():
    preorder = [3, 9, 20, 15, 7]
    inorder = [9, 3, 15, 20, 7]
    expect = TreeNode.stringToTreeNode("[3,9,20,null,null,15,7]")
    actual = solution.buildTree(preorder, inorder)
    assert TreeNode.isSame(actual, expect)
Пример #20
0
def test_isValidBST2():
  root = TreeNode.stringToTreeNode("[5,1,4,null,null,3,6]")
  actual = solution.isValidBST(root)
  assert not actual
Пример #21
0
def test_preorderTraversal3():
    root = TreeNode.stringToTreeNode("[]")
    expect = []
    actual = solution.preorderTraversal(root)
    assert actual == expect
Пример #22
0
def test_isValidBST3():
  root = TreeNode.stringToTreeNode("[1,1,1]")
  actual = solution.isValidBST(root)
  assert not actual
Пример #23
0
def test_rightSideView1():
    root = TreeNode.stringToTreeNode("[1,2,3,null,5,null,4]")
    expect = [1, 3, 4]
    actual = solution.rightSideView(root)
    assert actual == expect
Пример #24
0
def test_isValidBST4():
  root = TreeNode.stringToTreeNode("[0,null,-1]")
  actual = solution.isValidBST(root)
  assert not actual