예제 #1
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()
예제 #2
0
    def test_1(self):
        # Given
        s_root = TreeNode(3)
        s_left = TreeNode(4)
        s_right = TreeNode(5)
        s_left_left = TreeNode(1)
        s_left_right = TreeNode(2)

        s_root.left = s_left
        s_root.right = s_right
        s_root.left.left = s_left_left
        s_root.left.right = s_left_right

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

        t_root.left = t_left
        t_root.right = t_right

        # When
        output = self.sut.isSubtree(s_root, t_root)

        # Then
        self.assertEqual(True, output)
예제 #3
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
예제 #4
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)
예제 #5
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)
예제 #6
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
예제 #7
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)
예제 #8
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)
 def invertTree(self, root: TreeNode) -> TreeNode:
     if root is None:
         return None
     temp = root.right
     root.right = self.invertTree(root.left)
     root.left = self.invertTree(temp)
     return root
예제 #10
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
예제 #11
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)
예제 #12
0
 def mirrorTree(self, root: TreeNode) -> TreeNode:
     # 为空返回
     if not root: return
     # 保存左节点,就像a,b互换值需要一个临时变量
     temp = root.left
     root.left = self.mirrorTree(root.right)
     root.right = self.mirrorTree(temp)
     return root
예제 #13
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
 def helper(root: TreeNode) -> TreeNode:
     if root:
         helper(root.left)
         helper(root.right)
         if root:
             if root.left and root.left.val == 0 and not root.left.left and not root.left.right:
                 root.left = None
             if root.right and root.right.val == 0 and not root.right.right and not root.right.left:
                 root.right = None
        def helper(root: TreeNode, val: int) -> TreeNode:
            if not root:
                return TreeNode(val)

            if val > root.val:
                root.right = helper(root.right, val)
            else:
                root.left = helper(root.left, val)
            return root
예제 #16
0
 def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
     if not preorder:
         return None
     val = preorder[0]
     k = inorder.index(val)
     root = TreeNode(val)
     root.left = self.buildTree(preorder[1:k + 1], inorder[:k])
     root.right = self.buildTree(preorder[k + 1:], inorder[k + 1:])
     return root
예제 #17
0
def test():
    root = TreeNode(5)
    root.left = TreeNode(6)
    root.right = TreeNode(1)

    # pudb.set_trace()

    res = max_avg(root)
    assert res == 6
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root:
            return TreeNode(val)
        if root.val >= val:
            root.left = self.insertIntoBST(root.left, val)
        else:
            root.right = self.insertIntoBST(root.right, val)

        return root
예제 #19
0
 def helper(nums: List[int], start: int, end: int):
     if start <= end:
         value = max(nums[start:end + 1])
         index = nums.index(value)
         node = TreeNode(value)
         node.left = helper(nums, start, index - 1)
         node.right = helper(nums, index + 1, end)
         return node
     else:
         return None
예제 #20
0
def tree1():
    root = TreeNode(4)
    root.left = TreeNode(2)
    root.right = TreeNode(6)
    root.left.left = TreeNode(1)
    root.left.right = TreeNode(3)
    root.right.left = TreeNode(5)
    root.right.right = TreeNode(7)

    return root
예제 #21
0
def tree():
    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.left.left = TreeNode(4)
    root.right.left = TreeNode(2)
    root.right.left.left = TreeNode(4)
    root.right.right = TreeNode(4)

    return root
예제 #22
0
    def removeLeafNodes(self, root: TreeNode, target: int) -> TreeNode:
        if not root:
            return None

        root.left = self.removeLeafNodes(root.left, target)
        root.right = self.removeLeafNodes(root.right, target)
        if not root.left and not root.right and root.val == target:
            return None
        else:
            return root
예제 #23
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)
예제 #24
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
 def flatten(self, root: TreeNode) -> None:
     """
     Do not return anything, modify root in-place instead.
     """
     if not root:
         return None
     self.flatten(root.right)
     self.flatten(root.left)
     root.right = self.pre
     root.left = None
     self.pre = root
예제 #26
0
    def test2(self):
        root = TreeNode('a')
        root.left = TreeNode('b')
        root.right = TreeNode('c')
        root.left.left = TreeNode('d')

        Solution().invert(root)
        result = []
        self.preorder_list(root, result)

        self.assertEqual(result, ['a', 'c', 'b', 'd'])
예제 #27
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
예제 #28
0
    def test1(self):
        root = TreeNode(8)
        root.left = TreeNode(4)
        root.right = TreeNode(12)

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

        root.right.left = TreeNode(10)
        root.right.right = TreeNode(14)

        self.assertEqual(Solution().findCeilingFloor(root, 5), [4, 6])
예제 #29
0
 def helper(preorder: List[int], inorder: List[int], preleft: int,
            preright: int, inleft: int, inright: int):
     if inleft > inright:
         return None
     val = preorder[preleft]
     mid = inorder.index(val)
     root = TreeNode(val)
     root.left = helper(preorder, inorder, preleft + 1,
                        mid - inleft + preleft, inleft, mid - 1)
     root.right = helper(preorder, inorder, mid - inleft + preleft + 1,
                         preright, mid + 1, inright)
     return root
예제 #30
0
def test_3():
    sol = Solution()
    a2 = TreeNode(2)
    a1 = TreeNode(1)
    a4 = TreeNode(4)
    a2.left, a2.right = a1, a4
    a6 = TreeNode(6)
    a1.right = a6
    a3 = TreeNode(3)
    a5 = TreeNode(5)
    a4.left, a4.right = a3, a5
    assert not sol.isValidBST(a2)
예제 #31
0
 def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
     if not postorder:
         return None
     # 从后续遍历中获取根节点
     v = postorder[-1]
     # 从中序遍历中获取左右子树的分割点
     n = inorder.index(v)
     # 创建根节点
     root = TreeNode(v)
     # 根据n来构建二叉树进行分割递归
     root.left = self.buildTree(inorder[:n], postorder[:n])
     root.right = self.buildTree(inorder[n + 1:], postorder[n:-1])
     return root
예제 #32
0
def insertNode(root: TreeNode, num: int):
    if num > root.value:
        if root.right is None:
            root.right = TreeNode(num)
            return
        else:
            insertNode(root.right, num)
    if num < root.value:
        if root.left is None:
            root.left = TreeNode(num)
            return
        else:
            insertNode(root.left, num)
예제 #33
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
예제 #34
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)
예제 #35
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)
    def increasingBST(self, root: TreeNode) -> TreeNode:
        res = []

        def helper(root: TreeNode) -> List[int]:
            if not root:
                return
            helper(root.left)
            res.append(root.val)
            helper(root.right)
            return res

        helper(root)
        current = TreeNode(res[0])
        root = current
        for index, value in enumerate(res):
            if index + 1 >= len(res):
                break
            current.right = TreeNode(res[index + 1])
            current = current.right

        return root
예제 #37
0
파일: test_.py 프로젝트: ftlka/problems
# first test
root = TreeNode(3)
left, right = TreeNode(5), TreeNode(1)
left.left, left.right = TreeNode(6), TreeNode(2)
left.right.left, left.right.right = TreeNode(7), TreeNode(4)
right.left, right.right = TreeNode(0), TreeNode(8)
root.left, root.right = left, right

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

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

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

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

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

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

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

assert s.lowestCommonAncestor(root, root.left.right, root.right) == root
예제 #38
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)
예제 #39
0
def traverse(node, nodes):
    nodes.append(node.val)
    if node.left:
        traverse(node.left, nodes)
    if node.right:
        traverse(node.right, nodes)
    return nodes


s = Solution()

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

res = s.convertBST(root)

assert traverse(res, []) == [18, 20, 13]

# test 2
assert s.convertBST(None) is None

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

res = s.convertBST(root)
예제 #40
0
    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

answ_root = TreeNode(1)
answ_root.right, answ_root.right.right = TreeNode(1), TreeNode(1)
예제 #41
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("=====================")
예제 #42
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))
예제 #43
0
파일: test_.py 프로젝트: ftlka/problems
from solution import TreeNode, Solution


s = Solution()

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

assert s.inorderTraversal(root) == [1, 3, 2]


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

assert s.inorderTraversal(root) == [3, 2, 4, 1, 6, 5, 7]


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

assert s.inorderTraversal(root) == [1, 3, 2]
예제 #44
0
from solution import TreeNode, Solution


s = Solution()

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

assert s.tree2str(root) == '1(2(4))(3)'


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

assert s.tree2str(root) == '1(2()(4))(3)'


# third test
assert s.tree2str(None) == ''


# fourth test
root = TreeNode(1)
root.right = TreeNode(3)
예제 #45
0
파일: test_.py 프로젝트: ftlka/problems

	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


# test 2
root = TreeNode(1)
right = TreeNode(2)
right.right = TreeNode(3)
예제 #46
0
파일: test_.py 프로젝트: ftlka/problems
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