当前位置:网站首页>【二叉树】二叉树寻路
【二叉树】二叉树寻路
2022-07-06 22:41:00 【豪冷啊】
0x00 题目
在一棵无限的二叉树上,每个节点都有两个子节点
树中的节点 逐行 依次按 “之” 字形进行标记
如下图所示
在奇数行(即,第一行、第三行、第五行……)中,
按从左到右的顺序进行标记
而偶数行(即,第二行、第四行、第六行……)中,
按从右到左的顺序进行标记
给你树上某一个节点的标号 label
请你返回从根节点到该标号为 label 节点的路径
该路径是由途经的节点标号所组成的

粟子:
输入:label = 14
输出:[1,3,4,14]
0x01 思路
二叉树的每层的数量与高度相关
第 1 层,2 ^ 0 = 1,并且以 1 开头
第 2 层,2 ^ 1 = 2,并且以 2 开头
第 3 层,2 ^ 2 = 4,并且以 4 开头
二叉树按正常顺序展示时
1
2,3
4,5,6,7
子节点与父节点的关系为p = c / 2
根据这个规律
可以获取到从节点到根的路径上的所有节点
进而根据数量得到当前节点的高度
然后再对需要反转的层进行处理
根据高度可以求出一层中
最左边L 和最右边R 的值
还有一个当前值C
如何求出C的对称值V呢?
因为反转后是对称的C 比 L 大多少V 就比 R 小多少
所以V = R - (C - L) = L + R - C
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 pathInZigZagTree(_ label: Int) -> [Int] {
var res: [Int] = []
// 获取到根的路径上的所有节点
var val = label
while val != 0 {
res.append(val)
val = val >> 1 // 除以2,相当于右移
}
// 倒序 [14,4,3,1] -> [1,3,4,14]
res = Array(res.reversed())
var count = res.count - 2
while count > 0 {
// 左边节点值
let left = Int(pow(2.0, Float(count)))
// 右边节点值
let right = left + left - 1
// 当前要替换的节点值
var val = res[count]
// 计算替换的值
val = left + right - val
// 替换
res[count] = val
// 往上,跳过一层
count -= 2
}
return res
}
0x03 我的作品
欢迎体验我的作品之一:小五笔小程序-XWubi
五笔学习好帮手微信 搜索即可~
边栏推荐
- Acl2022 | decomposed meta learning small sample named entity recognition
- System framework of PureMVC
- [hand torn STL] list
- Using thread class and runnable interface to realize the difference between multithreading
- acwing 843. n-皇后问题
- Oracle - views and sequences
- JS variable plus
- Inventory host list in ansible (I wish you countless flowers and romance)
- Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
- Two methods of chromosome coordinate sequencing
猜你喜欢
随机推荐
Windows are not cheap things
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
Depth first traversal template principle of tree and graph
Flex layout and usage
Tree map: tree view - draw covid-19 array diagram
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
Error: No named parameter with the name ‘foregroundColor‘
STM32 encapsulates the one key configuration function of esp8266: realize the switching between AP mode and sta mode, and the creation of server and client
5G VoNR+之IMS Data Channel概念
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
STM32 system timer flashing LED
程序员上班摸鱼,这么玩才高端!
指针与数组在函数中输入实现逆序输出
装饰器基础学习02
R descriptive statistics and hypothesis testing
Detect when a tab bar item is pressed
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
acwing 843. n-皇后问题
为什么很多人对技术债务产生误解
关于01背包个人的一些理解









