当前位置:网站首页>[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 ~
边栏推荐
- Form validation and cascading drop-down lists (multiple implementations)
- 小程序 组件 定时器的清除
- Bugku,Web:都过滤了
- display 各值的区别
- 90. Subset II
- lotus 1.16.0 延长扇区过期时间
- Esp8266 Arduino programming example - deep sleep and wake up
- Alibaba cloud CDN practice
- Using Baidu easydl to realize chef hat recognition of bright kitchen and stove
- PCB材料简单介绍
猜你喜欢
![Introduction to C language [detailed]](/img/ac/9ba2e298faabd8dc4c76575ea182d1.png)
Introduction to C language [detailed]

Learning notes and summary of C language programming specification

拥抱开源指南

Lvs+keepalived high availability deployment practical application

Bugku,Web:都过滤了

HCIP(13)

成立不到一年!MIT衍生量子计算公司完成900万美元融资

39. Combined sum

SQL注入 Less42(POST型堆叠注入)

Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom
随机推荐
Clearing of applet component timer
get和post的区别
拥抱开源指南
Intranet penetration learning (III) horizontal movement of domain - planning tasks
Official document of kubevela 1.4.x
openresty 请求鉴权
typeof原理
使用webWorker执行后台任务
【NLP】生成词云
KubeVela 1.4.x 官方文档
HCIP(14)
HCIP(8)
Part 8: creating camera classes
HCIP(14)
Object.prototype.toString.call()的原理
Learn kotlin - extension function
什么是质因数,质因数(素因数或质因子)在数论里是指能整除给定正整数的质数
The person in charge of Tencent cloud database borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
Oracle triggers