Beispiel #1
0
def test_2():
    it = BSTIterator(TreeNode(1))
    ans = []
    while it.hasNext():
        ans.append(it.next())
    assert ans == [1]
Beispiel #2
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()
Beispiel #3
0
        idx += 1
        if idx == len(tree):
            break
        right = constructOne(tree[idx])
        idx += 1
        tn.right = right
        q.append(right)
    return root

def printNode(tn, indent):
    sb = ""
    for i in range(indent):
        sb += "\t"
    sb += str(tn.val)
    print(sb)

def printTree(root, indent):
    if not root:
        return
    printTree(root.right, indent + 1)
    printNode(root, indent)
    printTree(root.left, indent + 1)

#  root = createTree("1, 2, 5, 3, 4, #, 6")
root = createTree("4, 3, 5, 2, #, #, 7")
i, v = BSTIterator(root), []
while i.hasNext():
    v.append(i.next())
for node in v:
    print(node.val)
Beispiel #4
0
def test_0():
    it = BSTIterator(None)
    assert not it.hasNext()
    it = BSTIterator(TreeNode(1))
    assert it.next() == 1
    assert not it.hasNext()
Beispiel #5
0
        right = constructOne(tree[idx])
        idx += 1
        tn.right = right
        q.append(right)
    return root


def printNode(tn, indent):
    sb = ""
    for i in range(indent):
        sb += "\t"
    sb += str(tn.val)
    print(sb)


def printTree(root, indent):
    if not root:
        return
    printTree(root.right, indent + 1)
    printNode(root, indent)
    printTree(root.left, indent + 1)


#  root = createTree("1, 2, 5, 3, 4, #, 6")
root = createTree("4, 3, 5, 2, #, #, 7")
i, v = BSTIterator(root), []
while i.hasNext():
    v.append(i.next())
for node in v:
    print(node.val)