当前位置:网站首页>【二叉树】二叉树寻路
【二叉树】二叉树寻路
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
五笔学习好帮手微信 搜索即可~
边栏推荐
- Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
- Time complexity & space complexity
- ServiceMesh主要解决的三大痛点
- U++ 游戏类 学习笔记
- 【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
- Pointer and array are input in function to realize reverse order output
- Introduction to namespace Basics
- Tiktok may launch an independent grass planting community platform: will it become the second little red book
- DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)
- Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application
猜你喜欢
![[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III](/img/13/d598bb53b71fbadd4a97c603152124.png)
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III

Common Oracle SQL statements

System framework of PureMVC

Monitoring cannot be started after Oracle modifies the computer name

Pointer and array are input in function to realize reverse order output

MySQL数据库(基础篇)

C语言中函数指针与指针函数

DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)

Error: No named parameter with the name ‘foregroundColor‘

How to design API interface and realize unified format return?
随机推荐
Oracle - views and sequences
acwing 843. n-皇后问题
深入解析Kubebuilder
Time complexity & space complexity
Jetson nano配置pytorch深度学习环境//待完善
Common Oracle SQL statements
Two methods of chromosome coordinate sequencing
Flex layout and usage
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
Monitoring cannot be started after Oracle modifies the computer name
Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
Jetson nano configures pytorch deep learning environment / / to be improved
01机器学习相关规定
Read of shell internal value command
Why is the salary of test and development so high?
Using thread class and runnable interface to realize the difference between multithreading
STM32F103 realize IAP online upgrade application
【愚公系列】2022年7月 Go教学课程 005-变量
PLC Analog output analog output FB analog2nda (Mitsubishi FX3U)
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!