Esempio n. 1
0
 def test1(self):
     s = Solution()
     node1 = TreeNode(1)
     node2 = TreeNode(2)
     node1.left = node2
     root = s.recoverTree(node1)
     self._inorder(root)
Esempio n. 2
0
def test_1():
    sol = Solution()
    a2 = TreeNode(2)
    a1 = TreeNode(1)
    a4 = TreeNode(4)
    a2.left, a2.right = a1, a4
    a3 = TreeNode(3)
    a5 = TreeNode(5)
    a4.left, a4.right = a3, a5
    assert sol.isValidBST(a2)
Esempio n. 3
0
def test_2():
    sol = Solution()
    a2 = TreeNode(2)
    a6 = TreeNode(6)
    a4 = TreeNode(4)
    a2.left, a2.right = a6, a4
    a3 = TreeNode(3)
    a5 = TreeNode(5)
    a4.left, a4.right = a3, a5
    assert not sol.isValidBST(a2)
Esempio n. 4
0
def test_1():
    sol = Solution()
    a3 = TreeNode(3)
    a9 = TreeNode(9)
    a20 = TreeNode(20)
    a3.left, a3.right = a9, a20
    a15 = TreeNode(15)
    a7 = TreeNode(7)
    a20.left, a20.right = a15, a7
    assert sol.zigzagLevelOrder(a3) == [[3], [20, 9], [15, 7]]
Esempio n. 5
0
def test_2():
    sol = Solution()
    a1 = TreeNode(1)
    a0 = TreeNode(0)
    a1.right = a0
    sol.recoverTree(a1)

    t0 = TreeNode(0)
    t1 = TreeNode(1)
    t0.right = t1
    assert tree_eq(a1, t0)
Esempio n. 6
0
def test_1():
    sol = Solution()
    a1 = TreeNode(1)
    a0 = TreeNode(0)
    a3 = TreeNode(3)
    a1.left, a1.right = a0, a3
    a2 = TreeNode(2)
    a4 = TreeNode(4)
    a3.left, a3.right = a2, a4
    assert sol.maxDepth(a1) == 3
    assert sol.maxDepth(a0) == 1
    assert sol.maxDepth(a3) == 2
Esempio n. 7
0
def test_1():
    t4 = TreeNode(4)
    t2 = TreeNode(2)
    t6 = TreeNode(6)
    t4.left = t2
    t4.right = t6
    t3 = TreeNode(3)
    t2.right = t3
    t5 = TreeNode(5)
    t7 = TreeNode(7)
    t6.left = t5
    t6.right = t7
    it = BSTIterator(t4)
    assert it.hasNext()
    assert it.next() == 2
    assert it.hasNext()
    assert it.next() == 3
    assert it.hasNext()
    assert it.next() == 4
    assert it.hasNext()
    assert it.next() == 5
    assert it.hasNext()
    assert it.next() == 6
    assert it.hasNext()
    assert it.next() == 7
    assert not it.hasNext()
Esempio n. 8
0
 def test1(self):
     s = Solution()
     node1 = TreeNode(1)
     node2 = TreeNode(2)
     node3 = TreeNode(3)
     node4 = TreeNode(4)
     node5 = TreeNode(5)
     node6 = TreeNode(6)
     node7 = TreeNode(7)
     node4.left = node2
     node4.right = node6
     node2.left = node1
     node2.right = node3
     node6.left = node5
     node6.right = node7
     res = s.get_next_node(node4, node1)
     print res.val
Esempio n. 9
0
def test_3():
    sol = Solution()
    a1 = TreeNode(1)
    a3 = TreeNode(3)
    a1.right = a3
    a2 = TreeNode(2)
    a3.right = a2
    sol.recoverTree(a1)

    t1 = TreeNode(1)
    t2 = TreeNode(2)
    t1.right = t2
    t3 = TreeNode(3)
    t2.right = t3
    assert tree_eq(a1, t1)
Esempio n. 10
0
def test_2():
    sol = Solution()
    a = TreeNode(1)
    b1 = TreeNode(1)
    b2 = TreeNode(1)
    a.left, a.right = b1, b2
    c2 = TreeNode(1)
    b1.right = c2
    c4 = TreeNode(1)
    b2.right = c4
Esempio n. 11
0
    def test1(self):
        node1 = TreeNode(1)
        node2 = TreeNode(2)
        node3 = TreeNode(3)
        node4 = TreeNode(4)
        node5 = TreeNode(5)
        node6 = TreeNode(6)
        node7 = TreeNode(7)
        node1.left = node2
        node1.right = node3
        node2.left = node4
        node2.right = node5
        node3.left = node6
        node3.right = node7

        target = 7

        s = PathSum()
        res = s.get_all_paths(node1, target)
        # return res + '**'
        print res
Esempio n. 12
0
def test_1():
    t1 = TreeNode(1)
    t2 = TreeNode(2)
    t3 = TreeNode(3)
    t1.left = t2
    t1.right = t3
    t4 = TreeNode(4)
    t5 = TreeNode(5)
    t3.left = t4
    t3.right = t5
    _test_codec(t1)
Esempio n. 13
0
def test_2():
    a1 = TreeNode(1)
    a0 = TreeNode(0)
    a3 = TreeNode(3)
    a1.left, a1.right = a0, a3
    a2 = TreeNode(2)
    a4 = TreeNode(4)
    a3.left, a3.right = a2, a4

    b1 = TreeNode(1)
    b0 = TreeNode(0)
    b3 = TreeNode(3)
    b1.left, b1.right = b0, b3
    b2 = TreeNode(2)
    b4 = TreeNode(6)
    b3.left, b3.right = b2, b4

    sol = Solution()
    assert not sol.isSameTree(a1, b1)
Esempio n. 14
0
def test_1():
    sol = Solution()
    r = TreeNode(0)
    a1 = TreeNode(1)
    a2 = TreeNode(2)
    a3 = TreeNode(3)
    a4 = TreeNode(4)
    a5 = TreeNode(5)
    a6 = TreeNode(6)
    r.left, r.right = a1, a2
    a2.left, a2.right = a3, a6
    a3.left, a3.right = a4, a5
    assert sol.inorderTraversal(r) == [1, 0, 4, 3, 5, 2, 6]
Esempio n. 15
0
 def test3(self):
     s = Solution()
     node1 = TreeNode(1)
     node2 = TreeNode(2)
     node3 = TreeNode(3)
     node4 = TreeNode(4)
     node5 = TreeNode(5)
     node6 = TreeNode(6)
     node7 = TreeNode(7)
     node4.left = node2
     node4.right = node6
     node2.left = node1
     # node2.right = node3
     node6.left = node5
     node6.right = node7
     res = s.get_next_node(node4, node2)
     print res
     res = s.get_next_node(node4, node1)
     self.assertEqual(node2, res)
     print res
Esempio n. 16
0
def test_1():
    sol = Solution()
    a = TreeNode(1)
    b1 = TreeNode(2)
    b2 = TreeNode(2)
    a.left, a.right = b1, b2
    c1 = TreeNode(3)
    c2 = TreeNode(4)
    b1.left, b1.right = c1, c2
    c3 = TreeNode(4)
    c4 = TreeNode(3)
    b2.left, b2.right = c3, c4
    assert sol.isSymmetric(a)
Esempio n. 17
0
def test_1():
    n1 = TreeNode(1)
    n2 = TreeNode(2)
    n3 = TreeNode(3)
    n1.left = n2
    n1.right = n3
    n4 = TreeNode(4)
    n5 = TreeNode(5)
    n3.left = n4
    n3.right = n5
    assert sol.lowestCommonAncestor(n1, n4, n5) is n3
    assert sol.lowestCommonAncestor(n1, n2, n4) is n1
    assert sol.lowestCommonAncestor(n1, n2, n5) is n1
Esempio n. 18
0
    def test1(self):
        node1 = TreeNode(1)
        node2 = TreeNode(2)
        node3 = TreeNode(3)
        node4 = TreeNode(4)
        node5 = TreeNode(5)
        node6 = TreeNode(6)
        node7 = TreeNode(7)
        root = node4
        root.left = node2
        root.right = node6
        node2.left = node1
        node2.right = node3
        node6.left = node5
        node6.right = node7
        s = Solution()
        res = s.find_kth(root, 2)
        self.assertEqual(node2, res)

        res = s.find_kth(root, 3)
        self.assertEqual(node3, res)

        res = s.find_kth(root, 1)
        self.assertEqual(node1, res)
Esempio n. 19
0
def test_2():
    t1 = TreeNode(1)
    t2 = TreeNode(2)
    t3 = TreeNode(3)
    t1.left = t2
    t1.right = t3
    t4 = TreeNode(4)
    t2.left = t4
    t5 = TreeNode(5)
    t3.right = t5
    t6 = TreeNode(6)
    t4.right = t6
    t7 = TreeNode(7)
    t5.left = t7
    _test_codec(t1)
Esempio n. 20
0
from solution import Tree, TreeNode, MinHeap, convert_to_minHeap

n1 = TreeNode(1)
n2 = TreeNode(2)
n3 = TreeNode(3, n1, n2)
n4 = TreeNode(4)
n5 = TreeNode(5)
n6 = TreeNode(6, n4, n5)
n7 = TreeNode(7, n3, n6)
tree1 = Tree(n7)

m1 = TreeNode(1)
m3 = TreeNode(3, n1, None)
tree2 = Tree(m3)

tree3 = Tree()

def test_solution():
  assert convert_to_minHeap(tree1)._heap == [1, 2, 3, 7, 6, 4, 5]
  assert convert_to_minHeap(tree2)._heap == [1, 3]
  assert convert_to_minHeap(tree3)._heap == []
Esempio n. 21
0
def constructOne(s):
    if s == '#':
        return None
    else:
        return TreeNode(int(s))
Esempio n. 22
0
from solution import TreeNode, Solution

s = Solution()

# first test
root = TreeNode(1)
left, right = TreeNode(1), TreeNode(1)
left.left, left.right = TreeNode(1), TreeNode(1)
right.right = TreeNode(1)
root.left, root.right = left, right

assert s.isUnivalTree(root)

# second test
assert s.isUnivalTree(None)

# third test
root = TreeNode(2)
left, right = TreeNode(2), TreeNode(2)
left.left, left.right = TreeNode(5), TreeNode(2)
root.left, root.right = left, right

assert not s.isUnivalTree(root)
Esempio n. 23
0
def createNode(s):
    s = s.strip()
    if s == '#':
        return None
    else:
        return TreeNode(int(s))
Esempio n. 24
0
from solution import TreeNode, Solution


s = Solution()


root = TreeNode(6)
root.left = TreeNode(2)
root.right = TreeNode(8)
root.left.right = TreeNode(4)

assert s.lowestCommonAncestor(root, root.left, root.right) == root

assert s.lowestCommonAncestor(root, root.left, root.left.right) == root.left

assert s.lowestCommonAncestor(root, root.right, root) == root
Esempio n. 25
0
def test_1():
    """ 0, 1, 2, 3, 4, 5 """
    sol = Solution()
    a1 = TreeNode(1)
    a5 = TreeNode(5)
    a3 = TreeNode(3)
    a1.left, a1.right = a5, a3
    a2 = TreeNode(2)
    a4 = TreeNode(4)
    a3.left, a3.right = a2, a4
    a0 = TreeNode(0)
    a4.right = a0
    sol.recoverTree(a1)

    t1 = TreeNode(1)
    t0 = TreeNode(0)
    t3 = TreeNode(3)
    t1.left, t1.right = t0, t3
    t2 = TreeNode(2)
    t4 = TreeNode(4)
    t3.left, t3.right = t2, t4
    t5 = TreeNode(5)
    t4.right = t5
    assert tree_eq(a1, t1)
Esempio n. 26
0
from solution import TreeNode, Solution

s = Solution()

root = TreeNode(1)
left, right = TreeNode(2), TreeNode(5)
left.left, left.right, right.right = TreeNode(3), TreeNode(4), TreeNode(6)
root.left, root.right = left, right

s.flatten(root)

ar = [1, 2, 3, 4, 5, 6]
for el in ar:
    assert root.val == el

    if root and root.right:
        root = root.right
Esempio n. 27
0
from solution import Solution, TreeNode

s = Solution()

# basic tests
# [3, 9, 20, null, null, 15, 7]
root = TreeNode(3)
root.left = TreeNode(9)
right = TreeNode(20)
right.left, right.right = TreeNode(15), TreeNode(7)
root.right = right

answer = [[3], [20, 9], [15, 7]]
actual = s.zigzagLevelOrder(root)
assert actual == answer

# empty tree
assert s.zigzagLevelOrder(None) == []

# empty in between
# [1, 2, 3, 4, null, null, 5]
root = TreeNode(1)
left = TreeNode(2)
right = TreeNode(3)
left.left, right.right = TreeNode(4), TreeNode(5)
root.left, root.right = left, right

answer = [[1], [3, 2], [4, 5]]
assert s.zigzagLevelOrder(root) == answer
Esempio n. 28
0
def constructOne(s):
    s = s.strip()
    if s == '#':
        return None
    else:
        return TreeNode(int(s))
Esempio n. 29
0
    def setUp(self):
        root = TreeNode(3)
        left = TreeNode(4)
        right = TreeNode(5)
        left_left = TreeNode(1)
        left_right = TreeNode(2)

        root.left = left
        root.right = right
        root.left.left = left_left
        root.left.right = left_right

        t_root = TreeNode(4)
        t_left = TreeNode(1)
        t_right = TreeNode(2)

        t_root.left = t_left
        t_root.right = t_right

        self.sut = EarliestAncestor(root)
Esempio n. 30
0
from solution import TreeNode, Solution


def rec_preorder(node, ar):
	ar.append(node.val)
	if node.left:
		rec_preorder(node.left, ar)
	if node.right:
		rec_preorder(node.right, ar)


s = Solution()


# test 1
root = TreeNode(1)
root.right = TreeNode(2)
root.right.left = TreeNode(3)

nodes = []
rec_preorder(root, nodes)
assert s.preorderTraversal(root) == nodes


# test 2
root = TreeNode(1)
left, right = TreeNode(2), TreeNode(3)
left.left, left.right = TreeNode(4), TreeNode(5)
right.left, right.right = TreeNode(6), TreeNode(7)
root.left, root.right = left, right
Esempio n. 31
0
def test_2():
    n3 = TreeNode(3)
    n5 = TreeNode(5)
    n1 = TreeNode(1)
    n3.left = n5
    n3.right = n1
    n6 = TreeNode(6)
    n2 = TreeNode(2)
    n5.left = n6
    n5.right = n2
    n7 = TreeNode(7)
    n4 = TreeNode(4)
    n2.left = n7
    n2.right = n4
    n0 = TreeNode(0)
    n8 = TreeNode(8)
    n1.left = n0
    n1.right = n8
    assert sol.lowestCommonAncestor(n3, n5, n1) is n3
    assert sol.lowestCommonAncestor(n3, n5, n4) is n5
    assert sol.lowestCommonAncestor(n3, n5, n7) is n5
    assert sol.lowestCommonAncestor(n3, n5, n6) is n5
    assert sol.lowestCommonAncestor(n3, n5, n5) is n5
Esempio n. 32
0
    ar.append(node.val)
    if node.left:
        traverse(node.left, ar)
    else:
        ar.append(None)
    if node.right:
        traverse(node.right, ar)
    else:
        ar.append(None)
    return ar


s = Solution()

# first test
root = TreeNode(1)
right = TreeNode(0)
right.left, right.right = TreeNode(0), TreeNode(1)
root.right = right

answ_root = TreeNode(1)
answ_root.right, answ_root.right.right = TreeNode(0), TreeNode(1)

assert traverse(s.pruneTree(root), []) == traverse(answ_root, [])

# second test
root = TreeNode(1)
left, right = TreeNode(0), TreeNode(1)
left.left, left.right = TreeNode(0), TreeNode(0)
right.left, right.right = TreeNode(0), TreeNode(1)
root.left, root.right = left, right
Esempio n. 33
0
from solution import TreeNode, Solution

node = TreeNode(5)
node.left = TreeNode(3)
node.left.left = TreeNode(2)
node.left.left.left = TreeNode(1)
node.left.right = TreeNode(4)
node.right = TreeNode(7)
node.right.left = TreeNode(6)
node.right.right = TreeNode(8)


def printNode(root):
    if root is None:
        return
    printNode(root.left)
    print(root.val)
    printNode(root.right)


print("=====================")
printNode(node)
print("=====================")
sol = Solution()
node = sol.deleteNode(node, 5)
print("=====================")
printNode(node)
print("=====================")
Esempio n. 34
0
 def test1(self):
     node1 = TreeNode(1)
     node3 = TreeNode(3)
     node5 = TreeNode(5)
     node4 = TreeNode(4)
     node2 = TreeNode(2)
     node7 = TreeNode(7)
     node8 = TreeNode(8)
     node9 = TreeNode(9)
     node1.left = node3
     node1.right = node5
     node3.left = node2
     node3.right = node4
     node2.left = node9
     node4.right = node8
     node5.right = node7
     print_by_level(node1)
Esempio n. 35
0
from solution import Solution
from solution import TreeNode
t1 = TreeNode(1)
t2 = TreeNode(None)
t3 = TreeNode(2)
t4 = TreeNode(3)
root = t1
#t1.left = t2
t1.right = t3
t3.left = t4
s = Solution()
print(s.inorderTraversal(root))
Esempio n. 36
0
def test_1():
    sol = Solution()
    tgt = []
    """
       1
        \
         3
        /
       2
    """
    a = TreeNode(1)
    c = TreeNode(3)
    a.right = c
    b = TreeNode(2)
    c.left = b
    tgt.append(a)

    """
        3
       /
      2
     /
    1
    """
    c = TreeNode(3)
    b = TreeNode(2)
    c.left = b
    a = TreeNode(1)
    b.left = a
    tgt.append(c)

    """
      3
     /
    1
     \
      2
    """
    c = TreeNode(3)
    a = TreeNode(1)
    c.left = a
    b = TreeNode(2)
    a.right = b
    tgt.append(c)

    """
      2
     / \
    1   3
    """
    b = TreeNode(2)
    a = TreeNode(1)
    c = TreeNode(3)
    b.left = a
    b.right = c
    tgt.append(b)

    """
    1
     \
      2
       \
        3
    """
    a = TreeNode(1)
    b = TreeNode(2)
    a.right = b
    c = TreeNode(3)
    b.right = c
    tgt.append(a)

    ans = sol.generateTrees(3)
    assert forest_equal(ans, tgt)
Esempio n. 37
0
from solution import TreeNode, Solution


s = Solution()


root = TreeNode(4)
left, right = TreeNode(2), TreeNode(7)
left.left, left.right = TreeNode(1), TreeNode(3)
right.left, right.right = TreeNode(6), TreeNode(9)
root.left, root.right = left, right

inverted_root = s.invertTree(root)

# im too lazy
assert inverted_root.val == 4
assert (inverted_root.left.val, inverted_root.right.val) == (7, 2)
assert (inverted_root.left.left.val, inverted_root.left.right.val) == (9, 6)
assert (inverted_root.right.left.val, inverted_root.right.right.val) == (3, 1)
Esempio n. 38
0

def traverse(node, nodes):
	nodes.append(node.val if node else None)


	if node:
		traverse(node.left, nodes)
		traverse(node.right, nodes)


codec = Codec()


# test 1
root = TreeNode(1)
root.left = TreeNode(2)
right = TreeNode(3)
right.left, right.right = TreeNode(4), TreeNode(5)
root.right = right

deserialized = codec.deserialize(codec.serialize(root))

original_nodes = []
deser_nodes = []
traverse(root, original_nodes)
traverse(deserialized, deser_nodes)

assert original_nodes == deser_nodes

Esempio n. 39
0
 def setUp(self):
     root = TreeNode(1)
     node1 = TreeNode(2)
     node2 = TreeNode(2)
     root.left = node1
     root.right = node2
     node3 = TreeNode(3)
     node4 = TreeNode(3)
     node5 = TreeNode(3)
     node6 = TreeNode(3)
     node1.left = node3
     node1.right = node4
     node2.left = node5
     node2.right = node6
     node7 = TreeNode(4)
     node8 = TreeNode(4)
     node9 = TreeNode(4)
     node10 = TreeNode(4)
     node11 = TreeNode(4)
     node12 = TreeNode(4)
     node3.left = node7
     node3.right = node8
     node4.left = node9
     node4.right = node10
     node5.left = node11
     node5.right = node12
     node13 = TreeNode(5)
     node14 = TreeNode(5)
     node6.left = node13
     node6.right = node14
Esempio n. 40
0
def test_solution():
  from solution import Tree, TreeNode, is_complete

  n1 = TreeNode(1)
  n2 = TreeNode(2)
  n3 = TreeNode(3, n1, n2)
  n4 = TreeNode(4)
  n5 = TreeNode(5)
  n6 = TreeNode(6, n4, n5)
  n7 = TreeNode(7, n3, n6)
  tree1 = Tree(n7)

  m1 = TreeNode(1)
  m3 = TreeNode(3, m1, None)
  tree2 = Tree(m3)

  p1 = TreeNode(1)
  p3 = TreeNode(3, None, p1)
  tree4 = Tree(p3)

  tree3 = Tree()
  assert is_complete(tree1) == True
  assert is_complete(tree2) == True
  assert is_complete(tree3) == True
  assert is_complete(tree4) == False