张炬死亡现场照片,空知英秋和久保带人,于忠福
现在有一棵合法的二叉树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度
输入的第一行表示节点的个数n(1 ≤ n ≤ 1000,节点的编号为0到n-1)组成,
下面是n-1行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号。
输出树的高度,为一个整数
# -*- coding:utf-8 -*-
import sys
def tree_height(root):
if len(tree[root]) == 0:
return 1
node_list = []
for i in tree[root]:
node_list.append(tree_height(i))
return max(node_list) + 1
if __name__ == "__main__":
n = sys.stdin.readline().strip()
tree = []
for i in range(int(n)):
tree.append([])
for i in range(int(n)-1):
father, child = map(int, sys.stdin.readline().strip().split())
if len(tree[father]) <= 1 :
tree[father].append(child)
print(tree_height(0))
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论