
小白专场-树的同构-python语言实现
发布日期:2021-05-09 05:35:52
浏览次数:8
分类:博客文章
本文共 2869 字,大约阅读时间需要 9 分钟。
目录
数据结构与算法_Python_C完整教程目录:
更新、更全的《数据结构与算法》的更新网站,更有python、go、人工智能教学等着你:<
一、题意理解
给定两棵树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()
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年04月10日 12时14分27秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Selenium系列(八) - 截取完整页面和截取指定元素并保存为图片
2021-05-09
Jenkins - 部署在Tomcat容器里的Jenkins,提示“反向代理设置有误”
2021-05-09
Jenkins(3)- 安装Jenkins过程中遇到问题的排查思路
2021-05-09
Cypress系列(14)- 环境变量详解
2021-05-09
Mysql 常用函数(42)- substring_index 函数
2021-05-09
Jmeter 常用函数(7)- 详解 __time
2021-05-09
Jmeter 常用函数(28)- 详解 __FileToString
2021-05-09
Jmeter 常用函数(29)- 详解 __eval
2021-05-09
centos7 - 安裝 Python 3.7
2021-05-09
用前端姿势玩docker【一】Docker通俗理解常用功能汇总与操作埋坑
2021-05-09
基于jquery的简洁树形折叠菜单
2021-05-09
adb shell 查看系统属性(用来判断特殊的操作系统)
2021-05-09
DDD(领域驱动设计)--战略设计
2021-05-09
Spread for ASP.NET技术白皮书
2021-05-09
深入解析 DataGrid 过滤功能
2021-05-09
TX Text Control文字处理教程(8)使用超链接
2021-05-09
响应式设计(Response Web Design)实践
2021-05-09
Asp.Net MVC4入门指南(4):添加一个模型
2021-05-09
我看TechEd 2012之技术热点
2021-05-09