当前位置:网站首页>[binary tree] flip the binary tree to match the preorder traversal
[binary tree] flip the binary tree to match the preorder traversal
2022-06-23 18:25:00 【It's so cold】
0x00 subject
Give you the root node of a binary tree root
There is... In the tree n Nodes
Each node has a different node
And in 1 To n Between the value of the
Give you another one by n It's worth
Make up the journey sequence voyage
Represents the expected binary tree The first sequence traversal result
By exchanging the left and right subtrees of nodes
Sure Flip Any node in the binary tree
Please flip least Nodes in the tree
Make the of a binary tree The first sequence traversal With the expected traversal travel voyage Match
If possible , Then return to Flipped List of values of all nodes
You can return the answers in any order
If not , Then return to the list [-1]
0x01 Ideas
because voyage It's a Before the order The result of traversal
So we do a binary tree Before the order Traverse
The node is empty , nothing Need to match , That's it matching
node value When they are not equal , No matching
Recursive child node , Do the same
see whether Need to flip
0x02 solution
Language :Swift
Tree node :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
}
}
solution :
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 }
// Value inequality , Mismatch
if r.val != voyage[index] { return false }
// Indexes
index += 1
// Traverse the left and right subtrees
if dfs(r.left) && dfs(r.right) {
return true
}
if dfs(r.right) && dfs(r.left) {
// There's a flip , Add a node
res.append(r.val)
return true
}
return false
}
let b = dfs(root)
if b {
return res
}
return [-1]
}
0x03 My work
Welcome to experience one of my works : Small five strokes
Wubi learning is a good helper App Store Search ~
边栏推荐
- Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based
- [sword finger offer] 46 Translate numbers into strings
- A set of code to launch seven golang web frameworks at the same time
- Kotlin practical skills you should know
- iMeta | 南农沈其荣团队发布微生物网络分析和可视化R包ggClusterNet
- leetcode刷题:哈希表03 (快乐数)
- Crmeb second open SMS function tutorial
- Baidu AI Cloud product upgrade Observatory in May
- 2022 t elevator repair test question bank and simulation test
- torch学习(一):环境配置
猜你喜欢
![[unity] instructions for beginners of textanimator plug-in](/img/aa/a406c70a28931ac138e65787a0aabd.png)
[unity] instructions for beginners of textanimator plug-in

2022年T电梯修理考试题库及模拟考试

Regular expression use graph bed

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

实用电路分析3

论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks

深入理解 padding

How to solve the problem that the esp8266-01s cannot connect to Huawei routers

暂停更新公告—行走的皮卡丘

如何利用好每天的时间高效复习?
随机推荐
【ESP8266-01s】獲取天氣,城市,北京時間
Regular expression use graph bed
Baidu AI Cloud product upgrade Observatory in May
leetcode刷题:哈希表07 (三数之和)
Prevent users from submitting repeatedly in the uniapp project
csdn涨薪秘籍之Jenkins集成allure测试报告全套教程
Rancher2.6全新Monitoring快速入门
Remote connection raspberry pie in VNC Viewer Mode
QML类型:Loader
Redis 集群
一元二次方程到规范场
Redis 集群
After the Computer College changed its examination, the College of Cyberspace Security also changed its examination! Nanjing University of technology computer postgraduate entrance examination
Practical circuit analysis 3
论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
【win10 VS2019 opencv4.6 配置参考】
【二叉树】翻转二叉树以匹配先序遍历
What does the science and technology interactive sand table gain popularity by virtue of
信用卡产品开发周期从23周缩短至9周,银行运维组织如何转向敏捷?
云原生行业应用崛起,从“可用”到“好用”有多远?