侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python树的同构学习笔记

Python  /  管理员 发布于 7年前   181

一、题意理解

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构的”。现给定两棵树,请你判断它们是否是同构的。

输入格式:输入给出2棵二叉树的信息:

先在一行中给出该树的结点树,随后N行

第i行对应编号第i个结点,给出该结点中存储的字母、其左孩子结点的编号、右孩子结点的编号

如果孩子结点为空,则在相应位置给出“-”

如下图所示,有多种表示的方式,我们列出以下两种:

二、求解思路

搜到一篇也是讲这个的,但是那篇并没有完全用到单向链表的方法,所以研究了一下,写了一个是完全用单向链表的方法:

其实应该有更优雅的删除整个单向列表的方法,比如头设为none,可能会改进下?

# python语言实现L1 = list(map(int, input().split()))L2 = list(map(int, input().split()))# 节点class Node:  def __init__(self, coef, exp):    self.coef = coef    self.exp = exp    self.next = None# 单链表class List:  def __init__(self, node=None):    self.__head = node  # 为了访问私有类  def gethead(self):    return self.__head  def travel(self):    cur1 = self.__head    cur2 = self.__head    if cur1.next != None:      cur1 = cur1.next    else:      print(cur2.coef, cur2.exp, end="")      return    while cur1.next != None:      print(cur2.coef, cur2.exp, end=" ")      cur1 = cur1.next      cur2 = cur2.next    print(cur2.coef, cur2.exp, end=" ")    cur2 = cur2.next    print(cur2.coef, cur2.exp, end="")  # add item in the tail  def append(self, coef, exp):    node = Node(coef, exp)    if self.__head == None:      self.__head = node    else:      cur = self.__head      while cur.next != None:        cur = cur.next      cur.next = nodedef addl(l1, l2):  p1 = l1.gethead()  p2 = l2.gethead()  l3 = List()  while (p1 is not None) & (p2 is not None):    if (p1.exp > p2.exp):      l3.append(p1.coef, p1.exp)      p1 = p1.next    elif (p1.exp < p2.exp):      l3.append(p2.coef, p2.exp)      p2 = p2.next    else:      if (p1.coef + p2.coef == 0):        p1 = p1.next        p2 = p2.next      else:        l3.append(p2.coef + p1.coef, p1.exp)        p2 = p2.next        p1 = p1.next  while p1 is not None:    l3.append(p1.coef, p1.exp)    p1 = p1.next  while p2 is not None:    l3.append(p2.coef, p2.exp)    p2 = p2.next  if l3.gethead() == None:    l3.append(0, 0)  return l3def mull(l1, l2):  p1 = l1.gethead()  p2 = l2.gethead()  l3 = List()  l4 = List()  if (p1 is not None) & (p2 is not None):    while p1 is not None:      while p2 is not None:        l4.append(p1.coef * p2.coef, p1.exp + p2.exp)        p2 = p2.next      l3 = addl(l3, l4)      l4 = List()      p2 = l2.gethead()      p1 = p1.next  else:    l3.append(0, 0)  return l3def L2l(L):  l = List()  L.pop(0)  for i in range(0, len(L), 2):    l.append(L[i], L[i + 1])  return ll1 = L2l(L1)l2 = L2l(L2)l3 = List()l3 = mull(l1, l2)l3.travel()print("")l3 = List()l3 = addl(l1, l2)l3.travel()

以上就是本次介绍的全部内容知识点,相关内容可以参阅下方知识点,感谢大家的支持。


  • 上一条:
    python中树与树的表示知识点总结
    下一条:
    python图形绘制奥运五环实例讲解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客