好玩!----遍历二叉树



[Copy to clipboard] [ - ]
CODE:
def isnum(a):
    if type(a)==type(1):
        return True
    return False
def first_root(a):
    print a[1],'-',
    if isnum(a[0]):
        print a[0],'-',
    else:
        first_root(a[0])
    if isnum(a[2]):
        print a[2],'-',
    else:
        first_root(a[2])
def middle_root(a):
    if isnum(a[0]):
        print a[0],'-',
    else:
        middle_root(a[0])
    print a[1],'-',
    if isnum(a[2]):
        print a[2],'-',
    else:
        middle_root(a[2])
def last_root(a):
    if isnum(a[0]):
        print a[0],'-',
    else:
        last_root(a[0])
    if isnum(a[2]):
        print a[2],'-',
    else:
        last_root(a[2])
    print a[1],'-',
btree=[[1,2,3],2,[1,2,3]]
print "first"
first_root(btree)
print
print "middle"
middle_root(btree)
print
print "last"
last_root(btree)