当前位置:网站首页>【二叉树】二叉树中的伪回文路径
【二叉树】二叉树中的伪回文路径
2022-07-28 20:30:00 【豪冷啊】
0x00 题目
给你一棵二叉树,每个节点的值为 1 到 9
我们称二叉树中的一条路径是 「伪回文」的
当它满足:路径经过的所有节点值的排列中存在一个回文序列
请你返回从根到叶子节点的所有路径中伪回文 路径的数目
0x01 思路
一个回文数有以下特征:
情况一:1221,数字次数是偶数次
情况二:121,只有一个数字出现奇数次
根据位运算的异或运算:任何一个数 nn ^ n = 0
可以通过异或运算消除偶数次的数
题目取值范围是1~9 所以可以用每一位代表一个数字3表示为:0000001005表示为:000010000
如果最后剩下的数是 3 和 5
那么就是: 000010100
有 2 个 1,偶数次
所以不是回文数
通过循环 右移 和 与运算来计算有多少个 1
(10100 >> 1) & 1
还有更简单的办法:
任何一个数:n
通过位运算 与运算&
n & (n - 1) = 0
10100 &
10011 =
10000
为不 0
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 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 }
// 异或运算
let n = val ^ (1 << root.val)
// 叶子节点
if root.left == nil && root.right == nil {
// 所有偶数次的数,都消除了
// 或者只剩下一个数
if n == 0 || (n & (n-1)) == 0 {
res += 1
}
return
}
// 递归左右子树
if let left = root.left {
dfs(left, n)
}
if let right = root.right {
dfs(right, n)
}
}
dfs(root, 0)
return res
}
0x03 我的小作品
欢迎体验我的作品之一:小五笔 86 版
五笔学习好帮手App Store 搜索即可~
边栏推荐
- The binary search boundary value processing based on leetcode35 is used to clarify the boundary value of the judgment condition using the idea of interval
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
- HCIP(8)
- [CS231N]Lecture_2:Image Classification pipelin
- kingbase中指定用户默认查找schema,或曰用户无法使用public schema下函数问题
- How to establish a decentralized community in Web3
- Clearing of applet component timer
- array_ diff_ The method of not comparing array values when Assoc element is an array
- [machine learning] naive Bayesian classification of text -- Classification of people's names and countries
- What is a prime factor? In number theory, a prime factor (prime factor or prime factor) refers to a prime number that can divide a given positive integer
猜你喜欢

字节一面:TCP 和 UDP 可以使用同一个端口吗?

【机器学习】朴素贝叶斯对文本分类--对人名国别分类

39. Combined sum

From Web3 to web2.5, is it backward or another way?

Desai wisdom number - line chart (stacking area chart): ranking of deposits of different occupational groups in the proportion of monthly income in 2022

内网渗透学习(三)域横向移动——计划任务

90. Subset II

For the first time, Chinese scientists used DNA to construct convolutional artificial neural network, which can complete 32 types of molecular pattern recognition tasks, or be used for biomarker signa

Practice and exploration of overseas site Seata of ant group

从 Web3到Web2.5,是倒退还是另辟蹊径?
随机推荐
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
Aimbetter insight into your database, DPM and APM solutions
Save 70% of the video memory and increase the training speed by 2 times! Zheda & Ali proposed online convolution re parameterization orepa, and the code has been open source! (CVPR 2022 )
Lt7911d type-c/dp to Mipi scheme is mature and can provide technical support
Brief introduction to PCB materials
Summary of the use of hash table set and map when leetcode brushes questions
msfvenom制作主控与被控端
array_diff_assoc 元素是数组时不比较数组值的办法
Ukrainian officials: half of Ukrainian agricultural products are exported through the Danube port
小程序 canvas 生成海报
Apifox:满足你对 Api 的所有幻想
Data interpolation -- normalize data of different magnitude
Which brand is the best and most cost-effective open headset
从 Web3到Web2.5,是倒退还是另辟蹊径?
乌官员:乌克兰一半农产品经多瑙河港口出口
How to establish a decentralized community in Web3
Two global variables__ Dirname and__ Further introduction to common functions of filename and FS modules
LVS+KeepAlived高可用部署实战应用
什么是质因数,质因数(素因数或质因子)在数论里是指能整除给定正整数的质数
C语言编程规范学习笔记和总结