题目如下:

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.

Example 1:

Input: label = 14
Output: [1,3,4,14]

Example 2:

Input: label = 26
Output: [1,2,6,10,26]

Constraints:

  • 1 <= label <= 10^6

解题思路:先把正常的路径(即不是蛇形排列的数)求出来,接下来自底向顶遍历,偶数行的值需要交换,交换的逻辑也很简单,求出该层最左边和最右边的节点的number,记为low和一个high,那么对于number为inx的节点,其交换后的number就是: (low + high) - inx。

代码如下:

class Solution(object):def pathInZigZagTree(self, label):""":type label: int:rtype: List[int]"""res = []level = 0while label != 0:res.insert(0,label)label = label/2level += 1swap = Falsewhile level > 0:if swap:low = 2**(level-1)high = (low * 2 - 1)res[level-1] = (low + high) - res[level-1]swap = not swaplevel -= 1return res

转载于:https://www.cnblogs.com/seyjs/p/11131279.html

【leetcode】1104. Path In Zigzag Labelled Binary Tree相关推荐

  1. LeetCode 1104. Path In Zigzag Labelled Binary Tree解题报告

    1104. Path In Zigzag Labelled Binary Tree Path In Zigzag Labelled Binary Tree python solution 题目描述 I ...

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

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

  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. php函数设置默认值,php函数如何设置默认值作为对象?
  2. 《高级无线网络—4G技术》——1.3 混合4G无线网络协议
  3. P5787 二分图 /【模板】线段树分治(线段树分治、并查集)
  4. Nodejs之旅开始
  5. win2003无法进入桌面_电脑桌面怎么建立便签,可以在电脑桌面显示的便签
  6. linux 删除桌面环境变量,linux下export命令添加、删除环境变量(转载)
  7. 万物皆可Graph | 当推荐系统遇上图神经网络(四)
  8. 3.4 java基础总结①常用类String②Stringbuffer③Stringbuilder
  9. 解决Linux系统下磁盘IO紧张的一种方法
  10. XMLHttpRequest 对象获取服务端数据
  11. 5分钟快速安装ASP.NET Core 网站运行环境
  12. 如何用命令行的形式进行仿真及do脚本的使用(仿真工具使用技巧)【Modesim/Questasim】
  13. python求一个数所有因数
  14. VC++6.0的简单使用!(保姆级)
  15. 数组中出现次数超过一半的数字
  16. 批量修改文件夹下文件的名字
  17. Python中将科学计数法(或以e为底的自然对数)字符串转换为float浮点数
  18. rtsp海康威视地址格式
  19. Python高级——正则表达式 re模块1.match方法
  20. 专访阿希链钱汉涛:应用型项目过多关注底层技术会得不偿失 |区块链捕手

热门文章

  1. 腾讯云申请免费ssl证书(1年有效期)
  2. 飞凌嵌入式AM62x核心板,赋能新一代HMI
  3. 绝对值编码器常见的故障有哪些 如何处理
  4. ACM-ICPC 2018 南京赛区网络预赛 J AC Challenge (状压dp)
  5. [深度学习] embedding 在test阶段遇到OOV怎么办
  6. BMS 产品控制策略和整车交互策略
  7. 中学教师计算机运用培训简报,第十中学“教育信息化能力提升”培训活动简报...
  8. JDBC 学习笔记1
  9. 2022-2028年中国光声成像系统行业市场调研分析及发展规模预测报告
  10. 成像系统(一):A Review of Indocyanine Green Fluorescent Imaging in Surgery