py,dfs两个点RE,bfs过了,没想通问题在哪
查看原帖
py,dfs两个点RE,bfs过了,没想通问题在哪
355685
LIQYSA楼主2023/5/19 14:36

dfs代码,#1和#4测试点RE,没想明白有什么情况没考虑到:

def f(k, h):
    if lstl[k]:
        if h == maxh[0]:
            maxh[0] += 1
        f(lstl[k], h + 1)
    if lstr[k]:
        if h == maxh[0]:
            maxh[0] += 1
        f(lstr[k], h + 1)
n = int(input().strip())
if not n: print(0)
else:
    lstl = [0] * (n + 1)
    lstr = [0] * (n + 1)
    for i in range(1, n + 1):
        l, r = map(int, input().strip().split())
        if l: lstl[i] = l
        if r: lstr[i] = r
    maxh = [1]
    f(1, 1)
    print(maxh[0])
2023/5/19 14:36
加载中...