当前位置:网站首页>【二叉树】翻转二叉树以匹配先序遍历
【二叉树】翻转二叉树以匹配先序遍历
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 搜索即可~
边栏推荐
- [win10 vs2019 opencv4.6 configuration reference]
- Async/await
- 论文阅读 (57):2-hydr_Ensemble: Lysine 2-Hydroxyisobutyrylation Identification with Ensemble Method (任务)
- Pause update Bulletin - walking Pikachu
- How to solve the problem that the esp8266-01s cannot connect to Huawei routers
- 论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...
- Reading papers (51):integration of a holonic organizational control architecture and multiobjective
- 论文阅读 (48):A Library of Optimization Algorithms for Organizational Design
- Kdevtmpfsi processing of mining virus -- Practice
- Counter attack and defense (2): counter strategy against samples
猜你喜欢

Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints
![[Wwise] there is no sound problem after Wwise is embedded in unity and packaged](/img/70/4131671f5dfd36324cbe9bacea6ac4.png)
[Wwise] there is no sound problem after Wwise is embedded in unity and packaged

CRMEB 二开短信功能教程

README

2022 t elevator repair test question bank and simulation test
![Wechat applet reports an error [app.json file content error] app json: app. JSON not found](/img/ab/5c27e1bb80ad662d1a220d29c328e0.png)
Wechat applet reports an error [app.json file content error] app json: app. JSON not found

Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based

《致敬百年巨匠 , 数藏袖珍书票》

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

Alien world, real presentation, how does the alien version of Pokemon go achieve?
随机推荐
Baidu AI Cloud product upgrade Observatory in May
7、VLAN-Trunk
【ESP8266-01s】獲取天氣,城市,北京時間
SimpleDateFormat在多线程环境下存在线程安全问题。
How to make validity table
论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints
[failure announcement] there is a problem with the redis that replaces memcached, causing the website to fail
全局组织结构控制之抢滩登陆
Call face recognition exception
6 steps to teach you financial data mining preprocessing
README
【華中科技大學】考研初試複試資料分享
Troubleshooting and modification process of easycvr interface dislocation in small screen
[unity] instructions for beginners of textanimator plug-in
【华中科技大学】考研初试复试资料分享
Implementing Domain Driven Design - using ABP framework - General guidelines
2022年T电梯修理考试题库及模拟考试
【剑指Offer】45. 把数组排成最小的数
The battlefield of live broadcast e-commerce is not in the live broadcast room
torch学习(一):环境配置