当前位置:网站首页>[binary tree] pseudo palindrome path in binary tree
[binary tree] pseudo palindrome path in binary tree
2022-07-28 22:17:00 【How cold it is】
0x00 subject
Here is a binary tree for you , The value of each node is 1 To 9
We call a path in a binary tree 「 Pseudo palindrome 」 Of
When it satisfies : In the arrangement of all node values that the path passes through There is One Palindrome Sequence
Please return from root To leaf All paths of nodes Pseudo palindrome The path of number
0x01 Ideas
A palindrome number has the following characteristics :
Situation 1 :1221, Numbers frequency yes even numbers Time
Situation two :121, Only One The numbers appear Odd number Time
XOR operation based on bit operation : Any number nn ^ n = 0
Can pass Exclusive or operation eliminate Even numbers
The range of topics is 1~9 So it can be used every Represents a number 3 Expressed as :0000001005 Expressed as :000010000
If the last remaining number is 3 and 5
So that is : 000010100
Yes 2 individual 1, Even number of times
therefore No It's palindrome number
Through the loop Move right and And operation To calculate how many 1
(10100 >> 1) & 1
And more Simple The way to :
Any number :n
Bit operation And operation &
n & (n - 1) = 0
10100 &
10011 =
10000
Why not 0
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 pseudoPalindromicPaths (_ root: TreeNode?) -> Int {
guard let root = root else { return 0 }
var res: Int = 0
func dfs(_ root: TreeNode?, _ val: Int) {
guard let root = root else { return }
// Exclusive or operation
let n = val ^ (1 << root.val)
// Leaf node
if root.left == nil && root.right == nil {
// All even numbers , It's all eliminated
// Or just one number left
if n == 0 || (n & (n-1)) == 0 {
res += 1
}
return
}
// Recursive left and right subtrees
if let left = root.left {
dfs(left, n)
}
if let right = root.right {
dfs(right, n)
}
}
dfs(root, 0)
return res
}
0x03 My little work
Welcome to experience one of my works : Small five strokes 86 edition
Wubi learning is a good helper App Store Search ~
边栏推荐
猜你喜欢

HCIP(14)

Using Baidu easydl to realize chef hat recognition of bright kitchen and stove

Alibaba cloud CDN practice

CDN working principle

迪赛智慧数——折线图(堆叠面积图):2022年不同职业人群存款额占月收入比例排名
![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints

熊市下 DeFi 的未来趋势

Lvs+keepalived high availability deployment practical application

hcip实验(14)

成立不到一年!MIT衍生量子计算公司完成900万美元融资
随机推荐
Form validation and cascading drop-down lists (multiple implementations)
40. 组合总和 II
System Analyst
HYDAC溢流阀DB08A-01-C-N-500V
第 8 篇:创建摄像机类
使用webWorker执行后台任务
C语言编程规范学习笔记和总结
行内元素和块级元素有什么区别?语义化作用
LCR测试仪最为主要的功能和用途都是什么
CDN工作原理
ESP8266-Arduino编程实例-深度休眠与唤醒
hcip实验(12)
HYDAC overflow valve db08a-01-c-n-500v
熊市下 DeFi 的未来趋势
2021数学建模B组练习
HCIP(15)
如何在 Web3中建立一个去中心化社区
Oracle database objects
记录Flutter解决A RenderFlex overflowed by 7.3 pixels on the bottom溢出问题
HCIP(15)