1104. Path In Zigzag Labelled Binary Tree

  1. Path In Zigzag Labelled Binary Tree python solution

题目描述

In an infinite binary tree where every node has two children, the nodes are labelled in row order.
In the odd numbered rows (ie., the first, third, fifth,…), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,…), the labelling is right to left.

Given the label of a node in this tree, return the labels in the path from the root of the tree to the node with that label.

解析

首先分析正常顺序二叉树的父节点寻找方法,如下图所示

例如节点9,其父结点为[9/2]=4,此题仅将排列变为Zigzag。变换后的排列方式见下图

分析二者之间的关系。分析zigzag的变换规则,原节点和该层最小节点的距离等于变换后新节点和该层最大节点的距离。分别用x′x'x′和xxx代表zigzag中的节点和正常排序的原节点,则根据上述表达,可以得到公式:
x−min=max−x′x-min=max-x' x−min=max−x′
因此,x=max+min−x′x=max+min-x'x=max+min−x′。所以就得到了原节点和现节点的变换公式。解题思路就是先求的zigzag节点对应的原节点,除于二取整得到其父节点。重复此步骤,直到根节点。

// An highlighted block
class Solution:def pathInZigZagTree(self, label: int) -> List[int]:height=int(math.log(label,2))record=[label]while(height):min=2**heightmax=2**(height+1)-1orign_node=min+max-labelparent=int(orign_node/2)record.append(parent)label=parentheight-=1return record[::-1]

Reference

https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/discuss/324011/Python-O(logn)-time-and-space-with-readable-code-and-step-by-step-explanation

LeetCode 1104. Path In Zigzag Labelled Binary Tree解题报告相关推荐

  1. 1104. Path In Zigzag Labelled Binary Tree**

    1104. Path In Zigzag Labelled Binary Tree** https://leetcode.com/problems/path-in-zigzag-labelled-bi ...

  2. 【leetcode】1104. Path In Zigzag Labelled Binary Tree

    题目如下: In an infinite binary tree where every node has two children, the nodes are labelled in row or ...

  3. 1104 Path In Zigzag Labelled Binary Tree

    1 题目 In an infinite binary tree where every node has two children, the nodes are labelled in row ord ...

  4. LeetCode——1104. 二叉树寻路(Path In Zigzag Labelled Binary Tree)[中等]——分析及代码(Java)

    LeetCode--1104. 二叉树寻路[Path In Zigzag Labelled Binary Tree][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 按位置求解 (1 ...

  5. LeetCode-1104. Path In Zigzag Labelled Binary Tree

    LeetCode-1104. Path In Zigzag Labelled Binary Tree 题目描述:https://leetcode.com/problems/path-in-zigzag ...

  6. LeetCode每日一题(Path In Zigzag Labelled Binary Tree)

    In an infinite binary tree where every node has two children, the nodes are labelled in row order. I ...

  7. [Swift]LeetCode1104. 二叉树寻路 | Path In Zigzag Labelled Binary Tree

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. leetcode1104. Path In Zigzag Labelled Binary Tree

    题目链接 这个题很容易首先联想到完全二叉树的性质,父结点下标i,如果有左儿子,左儿子下标2i,如果有右儿子,右儿子下标2i+1,相应的一个结点的父结点就是i/2下取整.但是因为这个题在偶数行是从右到左 ...

  9. Path In Zigzag Labelled Binary Tree(C++二叉树寻路)

    解题思路: (1)从下往上,除2,注意左右顺序 class Solution { private:vector<int> v; public:void parent(int label) ...

最新文章

  1. R语言ggplot2可视化:使用长表数据(窄表数据)( Long Data Format)可视化多个时间序列数据、在同一个可视化图像中可视化多个时间序列数据(Multiple Time Series)
  2. 条件注释判断浏览器版本!--[if lt IE 9]
  3. 【php】目录操作 文件操作
  4. 在hive中对日期数据进行处理,毫秒级时间转化为yyyy-MM-dd格式
  5. Java 9 中的9个新特性
  6. 转 list三种遍历效率
  7. [翻译]Writing Custom Wizards 编写自定义的向导
  8. 手机MODEM开发(31)---LTE 速率低
  9. 抖音新特效:蚂蚁呀嘿安卓教程
  10. uint在c语言中的作用,C中int,Uint,uint16等有什么区别以及用处
  11. Android 使用MediaRecorder录音调用stop()方法的时候报错
  12. python拆分参数列表_Python序列拆分操作符与映射拆分操作符实例
  13. 三级面包屑的页面跳转到二级面包屑页面时,报错:Rendered fewer hooks than expected.
  14. 2020 dns排名_《2020年全球DNS威胁报告》:DNS攻击平均损失高达92万美元
  15. iis mysql密码_mysql忘记root密码与root帐号被删除处理方法
  16. 京东API详情接口调用示例
  17. 「学习笔记」自适应辛普森法
  18. 服务器连接硬盘出错,IBM服务器硬盘常见问题与故障诊断
  19. php时间戳与date格式转换
  20. 【笔记】效率脑科学:卓有成效地完成每一项工作

热门文章

  1. java事务是基于数据库的么_详解在Spring Boot中使用数据库事务
  2. 汉语言处理工具pyhanlp的拼音转换与字符正则化 1
  3. 详谈ORB-SLAM2的地图点MapPoint
  4. 整车下线流程(EOL)测试解决方案介绍
  5. Incorrect string value: '\xE7\xB3\xBB\xE7\xBB\x9F...' for column '字段名' at row 1,插入中文时...
  6. 动态获取bind dns日志IP脚本
  7. windows系统流氓软件太厉害卸载不掉怎么办?看我弄死它们
  8. 卸载流氓软件的整个过程
  9. HashMap集合常见方法
  10. 百度SEO站群素材资源解析平台系统源码V8.0