当前位置:网站首页>[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 ~
边栏推荐
- 2021 excellent TDP members' output data summary is coming, come and watch!!!
- 2022年T电梯修理考试题库及模拟考试
- [sword finger offer] 45 Arrange the array into the smallest number
- Alien world, real presentation, how does the alien version of Pokemon go achieve?
- 一元二次方程到规范场
- Counter attack and defense (2): counter strategy against samples
- 【ESP8266-01s】获取天气,城市,北京时间
- leetcode刷题:哈希表01 (有效的字母异位词)
- Ner's past, present and future Overview - Future
- 【ESP8266-01s】獲取天氣,城市,北京時間
猜你喜欢
![[unity] instructions for beginners of textanimator plug-in](/img/aa/a406c70a28931ac138e65787a0aabd.png)
[unity] instructions for beginners of textanimator plug-in

QML type: Loader

论文阅读 (56):Mutli-features Predction of Protein Translational Modification Sites (任务)

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

Leetcode: hash table 07 (sum of three numbers)

提高效率 Or 增加成本,开发人员应如何理解结对编程?

Redis 集群
![微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到](/img/ab/5c27e1bb80ad662d1a220d29c328e0.png)
微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到

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

科技互动沙盘是凭借什么收获人气的
随机推荐
论文阅读 (50):A Novel Matrix Game with Payoffs of Maxitive Belief Structure
[websocket] knowledge points for developing online customer service system meaning of status code returned by websocket
How to use R language to draw scatter diagram
leetcode刷题:哈希表02 (两个数组的交集)
【故障公告】取代 memcached 的 redis 出现问题造成网站故障
Prevent users from submitting repeatedly in the uniapp project
Self taught programming introduction, what language to learn first?
How do I write a small program that can automatically edit new year greetings
Video anomaly detection data set (shanghaitech)
Deploy LNMP environment and install Typecho blog
Latex使用\usepackage{hyperref}报错:paragraph ended before [email protected]@link was complete
After the Computer College changed its examination, the College of Cyberspace Security also changed its examination! Nanjing University of technology computer postgraduate entrance examination
mysql -- 经典面试题
Paper reading (50):a novel matrix game with payoffs of maximal belt structure
Dive into deep learning - 1. Introduction
对抗攻击与防御 (1):图像领域的对抗样本生成
Leetcode 1218. 最长定差子序列(提供一种思路)
反直觉的三门问题,80%的人都会错?
[win10 vs2019 opencv4.6 configuration reference]
Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints