当前位置:网站首页>【二叉树】翻转等价二叉树
【二叉树】翻转等价二叉树
2022-06-23 03:48:00 【豪冷啊】
0x00 题目
我们可以为二叉树 T 定义一个翻转操作
选择任意节点,然后交换它的左子树和右子树
只要经过一定次数的翻转操作后
能使 X 等于 Y
我们就称二叉树 X 翻转 等价 于二叉树 Y
这些树由根节点 root1 和 root2 给出
如果两个二叉树是翻转 等价 的
则返回 true ,否则返回 false
0x01 思路
节点都为空时,相等
其中一个为空,或者值不相等时
则不相等
对于子节点,可以选择翻转
或者不翻转
0x02 解法
语言:Swift
树节点:TreeNode
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
self.right = right
}
}
解法:
func flipEquiv(_ root1: TreeNode?, _ root2: TreeNode?) -> Bool {
// 包含为 空 的情况
if root1 === root2 { return true }
// 只有一个为空,或者值不相等
if root1 == nil || root2 == nil || root1?.val != root2?.val {
return false
}
// 不翻转的情况
let b1 = flipEquiv(root1?.left, root2?.left)
let b2 = flipEquiv(root1?.right, root2?.right)
// 翻转的情况
let b3 = flipEquiv(root1?.left, root2?.right)
let b4 = flipEquiv(root1?.right, root2?.left)
return (b1 && b2) || (b3 && b4)
}
0x03 我的作品
欢迎体验我的作品之一:小编辑器
小巧的在线编辑器
包含多种语言App Store 搜索即可~
边栏推荐
- P1363 幻象迷宫(dfs)
- Compilation, installation and global configuration section description of haproxy
- 摆烂LuoGu刷题记
- What is the difference between redistemplate and CacheManager operation redis
- Section 2: spingboot unit test
- Bug STM32 advanced timer (haha, to tell you the truth, the hardware timer can't reflect its strength. In fact, I want to send the kernel timer. Just think about it. Take your time)
- PTA:7-63 计算高考状元
- PTA:7-64 该日是该年的第几天
- 虫子 STM32 高级定时器 (哈哈我说实话硬件定时器不能体现实力,实际上想把内核定时器发上来的,一想算了,慢慢来吧)
- linux下的开源数据库是什么
猜你喜欢

mysql存储引擎之Myisam和Innodb的区别

Cool mouse following animation JS plug-ins 5

Analysis on the current situation of the Internet of things in 2022

Photoshop PS viewing pixel coordinates, pixel colors, pixel HSB colors

Background ribbon animation plug-in ribbon js

【二叉树进阶】AVLTree - 平衡二叉搜索树

Web page dynamic and static separation based on haproxy

8 key indicators to measure technology debt in 2022

Introduction to deep learning

Full analysis of embedded software testing tool tpt18 update
随机推荐
How e-commerce makes use of small programs
Background ribbon animation plug-in ribbon js
Pyspark, paid for data cleaning and uploading to the database
Online text filter less than specified length tool
Differences between MyISAM and InnoDB of MySQL storage engine
【二叉樹進階】AVLTree - 平衡二叉搜索樹
AI video cloud vs narrowband HD, who is the favorite in the video Era
Twitter cooperates with Shopify to introduce merchant products into twitter shopping
[从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
How to make the page number start from the specified page in word
Can MySQL be used in Linux
Twitter与Shopify合作 将商家产品引入Twitter购物当中
基于FPGA的VGA协议实现
Efficient remote office experience | community essay solicitation
Deploying Apache pulsar on kubesphere
摆烂LuoGu刷题记
Black horse PostgreSQL, why is it black in the end
Similar to RZ / SZ, trzsz supporting TMUX has released a new version
如何保证应用程序的安全性
[tcapulusdb knowledge base] [list table] example code for deleting the data at the specified location in the list