Python 題目

PROBLEM

二元樹

一題一題刷

def dfs_pre(T, index):
    n = len(T)
    left = index * 2 + 1
    right = index * 2 + 2
    if T[index] != "null":
        print(T[index], end=" ")
    if left < n:
        dfs_pre(T, left)
    if right < n:
        dfs_pre(T, right)
def dfs_in(T, index):
    n = len(T)
    left = index * 2 + 1
    right = index * 2 + 2
    if left < n:
        dfs_in(T, left)
    if T[index] != "null":
        print(T[index], end=" ")
    if right < n:
        dfs_in(T, right)
def dfs_post(T, index):
    n = len(T)
    left = index * 2 + 1
    right = index * 2 + 2
    if left < n:
        dfs_post(T, left)
    if right < n:
        dfs_post(T, right)
    if T[index] != "null":
        print(T[index], end=" ")

T = input().split()
dfs_pre(T, 0)
print()
dfs_in(T, 0)
print()
dfs_post(T, 0)

使用套件

import bisect

a = [1, 3, 3, 5, 7]
bisect.bisect_left(a, 3)  # 1
bisect.bisect_right(a, 3) # 3