当前位置:网站首页>【二叉树-中等】1104. 二叉树寻路
【二叉树-中等】1104. 二叉树寻路
2022-08-10 01:52:00 【菜菜2022】
【题目】
【代码】
class Solution:
def pathInZigZagTree(self, label: int) -> List[int]:
parents=[]
levels=[]
def levelVisit(label):
nonlocal levels
for level_id in range(1,1001):
level=[pow(2,level_id)+i for i in range(pow(2,level_id))]
levels.append(level)
if label in level:
break
levels=levels[::-1]
# print(levels)
for idx,level in enumerate(levels):
parents.append(label)
if label not in levels[idx]:
break
cur_pos=levels[idx].index(label)//2
# print("idx:",idx," cur_pos:",cur_pos)
if idx!=len(levels)-1:
label=levels[idx+1][len(levels[idx+1])-1-cur_pos]
if label==1:
return [1]
levelVisit(label)
parents.append(1)
# print(parents)
return parents[::-1]
【方法2】来自麦麦麦麦子。的题解
class Solution:
def pathInZigZagTree(self, label: int) -> List[int]:
res = []
while label != 1:
res.append(label)
label >>= 1
label = label ^(1 << (label.bit_length() - 1)) - 1
return [1]+res[::-1]
边栏推荐
- 中级xss绕过【xss Game】
- 实操|风控模型中常用的这三种预测方法与多分类场景的实现
- Interdepartmental Communication Skills
- Shader Graph learns various special effects cases
- 力扣每日一题-第51天-744. 寻找比目标字母大的最小字母
- 跨部门沟通的技巧
- 【SSRF漏洞】实战演示 超详细讲解
- OOD论文:Revisit Overconfidence for OOD Detection
- C# 正则表达式分组查询
- Problems and solutions related to Chinese character set in file operations in ABAP
猜你喜欢
随机推荐
进程管理和任务管理
彩色袜子题
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
中文NER的SOTA:RICON
常用正则备查
Unity vertex animation
Janus实际生产案例
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
Teach you how to write performance test cases
[Turn] Typora_Markdown_ picture title (caption)
In the 2022 gold, nine, silver and ten work tide, how can I successfully change jobs and get a high salary?
中级xss绕过【xss Game】
Problems and solutions related to Chinese character set in file operations in ABAP
OpenCV图像处理学习三,Mat对象构造函数与常用方法
【SSRF漏洞】实战演示 超详细讲解
Unity image is blurry after using long image
STM32F103驱动HCSR04超声波测距显示
Premint工具,作为普通人我们需要了解哪些内容?
基于设计稿识别的可视化低代码系统实践
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte