当前位置:网站首页>【二叉树】翻转二叉树以匹配先序遍历
【二叉树】翻转二叉树以匹配先序遍历
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 搜索即可~
边栏推荐
- 提高效率 Or 增加成本,开发人员应如何理解结对编程?
- A set of code to launch seven golang web frameworks at the same time
- 【win10 VS2019 opencv4.6 配置参考】
- [WebSocket] 开发在线客服系统知识点-websocket返回状态码的含义
- 【C工具】------点阵模拟测试2
- 深入理解 padding
- Landing of global organizational structure control
- 《致敬百年巨匠 , 数藏袖珍书票》
- Kotlin practical skills you should know
- 【 Huazhong University of Science and technology】 Data Sharing for retest of the initial Examination
猜你喜欢

Paper reading (56):muti features predction of protein translational modification sites (task)

Torch learning (I): environment configuration

Paper reading (54):deepfool: a simple and accurate method to four deep neural networks

【ESP8266-01s】获取天气,城市,北京时间

【Wwise】Wwise嵌入Unity后打包出现没有声音问题

Self training multi sequence learning with transformer for weakly supervised video animation

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

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

QML类型:Loader

Redis 集群
随机推荐
百度智能云5月产品升级观察站
微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到
一元二次方程到规范场
论文阅读 (57):2-hydr_Ensemble: Lysine 2-Hydroxyisobutyrylation Identification with Ensemble Method (任务)
CRMEB 二开短信功能教程
Regular expression use graph bed
论文阅读 (48):A Library of Optimization Algorithms for Organizational Design
Strong cache and negotiation cache in http
Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based
Redis Cluster
实现领域驱动设计 - 使用ABP框架 - 通用准则
Implementing Domain Driven Design - using ABP framework - repository
[websocket] knowledge points for developing online customer service system meaning of status code returned by websocket
手机开户一般哪个证券公司好?在线开户安全么?
Counter attack and defense (1): counter sample generation in image domain
Wiley- Open Science Joint Symposium of the documentation and information center of the Chinese Academy of Sciences, lecture 2: open access journal selection and paper submission
torch学习(一):环境配置
Dive into deep learning - 1. Introduction
Kdevtmpfsi processing of mining virus -- Practice
VNC Viewer方式的远程连接树莓派