当前位置:网站首页>【二叉树】翻转二叉树以匹配先序遍历
【二叉树】翻转二叉树以匹配先序遍历
2022-06-23 17:23:00 【豪冷啊】
0x00 题目
给你一棵二叉树的根节点 root
树中有 n 个节点
每个节点都有一个不同于其他节点
且处于 1 到 n 之间的值
另给你一个由 n 个值
组成的行程序列 voyage
表示预期的二叉树 先序遍历 结果
通过交换节点的左右子树
可以 翻转 该二叉树中的任意节点
请翻转 最少 的树中节点
使二叉树的 先序遍历 与预期的遍历行程 voyage 相匹配
如果可以,则返回 翻转的 所有节点的值的列表
你可以按任何顺序返回答案
如果不能,则返回列表 [-1]
0x01 思路
因为 voyage 是一个前序遍历的结果
所以对二叉树进行前序遍历
节点为空,无需匹配,那就是匹配
节点值不相等时,不匹配
递归子节点,进行相同操作
看是否需要翻转
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 flipMatchVoyage(_ root: TreeNode?, _ voyage: [Int]) -> [Int] {
var index = 0
var res: [Int] = []
func dfs(_ root: TreeNode?) -> Bool {
guard let r = root else { return false }
// 值不相等,不匹配
if r.val != voyage[index] { return false }
//索引
index += 1
// 遍历左右子树
if dfs(r.left) && dfs(r.right) {
return true
}
if dfs(r.right) && dfs(r.left) {
// 有翻转,添加节点
res.append(r.val)
return true
}
return false
}
let b = dfs(root)
if b {
return res
}
return [-1]
}
0x03 我的作品
欢迎体验我的作品之一:小五笔
五笔学习好帮手App Store 搜索即可~
边栏推荐
- Method of copying web page content and automatically adding copyright information (compatible with ie, Firefox and chrome)
- How to make validity table
- 对抗攻击与防御 (2):对抗样本的反制策略
- QML type: Loader
- What if the website is poisoned
- csdn涨薪秘籍之Jenkins集成allure测试报告全套教程
- 【剑指Offer】45. 把数组排成最小的数
- Dive Into Deep Learning——1、前言
- Counter attack and defense (1): counter sample generation in image domain
- Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints
猜你喜欢
![[win10 vs2019 opencv4.6 configuration reference]](/img/51/62fb26123561b65f127304ede834a2.png)
[win10 vs2019 opencv4.6 configuration reference]

全局组织结构控制之抢滩登陆

【故障公告】取代 memcached 的 redis 出现问题造成网站故障

【ESP8266-01s】獲取天氣,城市,北京時間

README

Imeta | Nannong shenqirong team released microbial network analysis and visualization R package ggclusternet

esp8266-01s 不能连接华为路由器解决方法

Customer service system building tutorial_ Installation and use mode under the pagoda panel_ Docking with official account_ Support app/h5 multi tenant operation

微信小程序startLocationUpdateBackground()简单实现骑手配送位置

Thesis reading (53):universal advantageous perturbations
随机推荐
csdn涨薪秘籍之Jenkins集成allure测试报告全套教程
Paper reading (50):a novel matrix game with payoffs of maximal belt structure
Troubleshooting and modification process of easycvr interface dislocation in small screen
[esp8266-01s] get weather, city, Beijing time
Wiley-中国科学院文献情报中心开放科学联合研讨会第二讲:开放获取期刊选择及论文投稿...
芯片原厂必学技术之理论篇(4-1)时钟技术、复位技术
A set of code to launch seven golang web frameworks at the same time
QML type: Loader
The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!
Nodejs implements multi process
CRMEB 二开短信功能教程
如何利用好每天的时间高效复习?
Theory of technology that must be learned by chip manufacturers (4-1) clock technology and reset Technology
esp8266-01s 不能连接华为路由器解决方法
论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
The battlefield of live broadcast e-commerce is not in the live broadcast room
Baidu AI Cloud product upgrade Observatory in May
[tool C] - lattice simulation test 2
提高效率 Or 增加成本,开发人员应如何理解结对编程?
leetcode刷题:哈希表07 (三数之和)