拼多多tree真恶心,输出格式调半天

rt 发下AC python解 写得真心丑

全部评论
参考楼主的写了一个递归的 class TreeNode(object): def __init__(self,val,parent): self.val = val self.child = [] self.parent = parent def SetPre(pre,s): pre=pre.replace('-'," ") pre=pre.replace('`'," ") return pre+s def PrintTree(root,pree): if root.parent == -1: print(root.val) else: print(pree+" "+root.val) if root.child: root.child.sort(key = lambda x:x.val) for ind,child in enumerate(root.child): if ind != len(root.child)-1: pre = SetPre(pree,"|--") PrintTree(child,pre) else: pre = SetPre(pree,"`--") PrintTree(child,pre) n = int(raw_input()) nodes = [] for i in xrange(n): fileParent = raw_input().split() node = TreeNode(fileParent[0],int(fileParent[1])) nodes.append(node) if int(fileParent[1]) != -1: nodes[int(fileParent[1])].child.append(node) PrintTree(nodes[0],"")
点赞 回复 分享
发布于 2017-09-04 11:51
这题过了??????????????
点赞 回复 分享
发布于 2017-09-02 17:20
用递归
点赞 回复 分享
发布于 2017-09-02 17:09
666,看见这题直接放弃了。。。
点赞 回复 分享
发布于 2017-09-02 17:08

相关推荐

06-14 19:09
门头沟学院 Java
darius_:给制造业搞的,什么物料管理生产管理,设备管理点检,最最关键的就是一堆报表看板。个人觉得没啥技术含量都是些基本的crud,但是业务很繁琐那种
点赞 评论 收藏
分享
07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务