python二叉树是怎么来的(Python二叉树的镜像转换实现方法示例)
类别:脚本大全 浏览量:1250
时间:2022-01-14 02:47:51 python二叉树是怎么来的
Python二叉树的镜像转换实现方法示例本文实例讲述了python二叉树的镜像转换实现方法。分享给大家供大家参考,具体如下:
问题描述
操作给定的二叉树,将其变换为源二叉树的镜像。
思路描述
1. 代码比文字更直观
2. 文字描述:新建一个二叉树,利用递归法,将源二叉树上的左节点赋值到新二叉树的右节点,将源二叉树上的右节点赋值到新二叉树的左节点。
python代码
|
# 方式1:生成新的镜像二叉树 def getmirrorbst( self , root): if root = = none: return newtree = treenode(root.val) newtree.right = self .getmirrorbst(root.left) newtree.left = self .getmirrorbst(root.right) return newtree |
但是提交代码后,说通过率为0… 原来要求将原有的二叉树就地改成镜像二叉树…如此一来,代码就更简单了:因为交换根节点的左右子节点时,以左右子节点为根节点的左子树和右子树也会交换位置。最终的python代码如下:
|
# 方式2:改变给定的二叉树为镜像二叉树 def turntomirror( self , root): if root = = none: return root.right, root.left = root.left, root.right self .turntomirror(root.left) self .turntomirror(root.right) return root |
包含测试代码的最终代码如下:
|
class solution: # 给定一个二叉树,获得其镜像(轴对称)的镜像二叉树: # 方式1:生成新的镜像二叉树 def getmirrorbst( self , root): if root = = none: return newtree = treenode(root.val) newtree.right = self .getmirrorbst(root.left) newtree.left = self .getmirrorbst(root.right) return newtree # 方式2:改变给定的二叉树为镜像二叉树 def turntomirror( self , root): if root = = none: return root.right, root.left = root.left, root.right self .turntomirror(root.left) self .turntomirror(root.right) return root # 给定二叉树的前序遍历和中序遍历,获得该二叉树 def getbstwithpretin( self , pre, tin): if len (pre) = = 0 | len (tin) = = 0 : return none root = treenode(pre[ 0 ]) for order,item in enumerate (tin): if root .val = = item: root.left = self .getbstwithpretin(pre[ 1 :order + 1 ], tin[:order]) root.right = self .getbstwithpretin(pre[order + 1 :], tin[order + 1 :]) return root class treenode: def __init__( self , x): self .left = none self .right = none self .val = x if __name__ = = '__main__' : flag = "turntomirror" solution = solution() preorder_seq = [ 1 , 2 , 4 , 7 , 3 , 5 , 6 , 8 ] middleorder_seq = [ 4 , 7 , 2 , 1 , 5 , 3 , 8 , 6 ] treeroot1 = solution.getbstwithpretin(preorder_seq, middleorder_seq) if flag = = "mirrorbst" : newroot = solution.getmirrorbst(treeroot1) print (newroot) if flag = = "turntomirror" : solution.turntomirror(treeroot1) print (treeroot1) |
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/u010005281/article/details/79473690
您可能感兴趣
- python 正则表达式语法大全(python re库的正则表达式入门学习教程)
- python全局变量设置(Python3.5局部变量与全局变量作用域实例分析)
- pythonssh登录服务器(对python 通过ssh访问数据库的实例详解)
- matlab嵌入python代码(python调用matlab的m自定义函数方法)
- python中输入背景颜色的代码(Python 给屏幕打印信息加上颜色的实现方法)
- python怎么实现链表(Python实现数据结构线性链表单链表算法示例)
- python如何将运行结果存入txt中(详解python读取和输出到txt)
- python封装函数讲解(Python中super函数用法实例分析)
- python 多线程与多进程(python 多线程串行和并行的实例)
- python图片识别文字代码(python批量识别图片指定区域文字内容)
- python opencv 标记目标(使用Python的OpenCV模块识别滑动验证码的缺口推荐)
- python3第三方库手册(使用python3构建文件传输的方法)
- python零基础入门五小时(从0开始的Python学习016异常)
- python调用支付宝支付接口(python实现支付宝转账接口)
- python3函数的使用方法(Python3.5基础之函数的定义与使用实例详解参数、作用域、递归、重载等)
- python元组和列表的定义(Python基本数据结构与用法详解列表、元组、集合、字典)
- 新款起亚霸锐实拍,比普拉多气派,配3.0T V6,引入国内或许能大卖(新款起亚霸锐实拍)
- ()
- 张勇 阿里新任掌门人(阿里新任掌门人)
- 毛戈平全国第一柜花落银泰 高端国货迎来 高光 时刻(毛戈平全国第一柜花落银泰)
- 14岁丧父 20岁丧母,从苦难走向辉煌的银泰创始人沈国军(14岁丧父20岁丧母从苦难走向辉煌的银泰创始人沈国军)
- 银泰集团董事长沈国军获评 北京影响力 十大企业家(银泰集团董事长沈国军获评)
热门推荐
- web服务器的概念及功能(web服务器是指什么?详解什么是web服务器)
- 护卫神主机大师使用教程(护卫神主机大师软件窗口显示不全的解决办法)
- python怎样读取mysql数据(使用Python将Mysql的查询数据导出到文件的方法)
- C#栈和堆的区别
- pythonselenium怎么设置元素(Python2 Selenium元素定位的实现8种)
- mysql查询性能优化详解(实例讲解MySQL 慢查询)
- dedecms自定义表单验证码(dedecms织梦验证码无法正常显示的3种解决办法)
- 如何把阿里云盘加入到快捷操作(阿里云如何挂载数据盘?阿里云挂载数据盘一键脚本)
- css3结合js制作(CSS3截取字符串实例代码推荐)
- filezilla搭建ftp服务器英文(客户端使用FileZilla连接ftp服务器过程图解)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9