1 题目

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]

2 尝试解

2.1 分析

一个无限大的完全二叉树,其值按Z字型从1依序递增1。给定一个值,问从根节点到该值所在节点的路径。

考虑同样的二叉树,但是值按照每层从左向右的顺序依序递增1。在此二叉树中,发现任意一个值为N的节点,其父节点的值为N/2。所以只需要重复取半,即可得到从该节点到根节点的路径。如给定14,路径为{14,7,3,1}。

              1                  1/ \                / \3   2              2   3/ \ / \            / \ / \4  5 6  7          4  5 6  7

现在考虑两棵二叉树之间的相互转化,给定值label及其所在的层数height(可由label求出),label'表示对应节点在另一棵二叉树中的值,有

height%2 == 0  label = label'

height%2 == 1 label+label' = 2^height + 2^(height-1) - 1

只需要对路径中的每个元素采用上述转化规则,即将一个路径转化为另一个对应路径。

2.2 代码

class Solution {
public:int transform(int label, int height){if(height % 2 == 1) return label;else return (1 << height) - label - 1 + (1 << (height-1));}vector<int> pathInZigZagTree(int label) {int height = 1;vector<int> result;while(label >= (1 << height)){height += 1;}label = transform(label,height);while(height > 0){result.push_back(transform(label,height));label /= 2;height--;}reverse(result.begin(),result.end());return result;}
};

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解题报告

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

  3. 【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 ...

  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. jQuery插件:超酷的多列网格式拖放插件gridster.js
  2. 对比BF245、2SK30A,2SK160A与2SK241对于150kHz导航信号放大关系
  3. android 自动登录机制,Android登录记住密码以及自动登录的实现
  4. QLibrary执行load失败
  5. qhfl-2 ContentType组件
  6. Spring+Mybatis使用MapperScannerConfigurer简化配置__MapperScannerConfigurer的作用
  7. 共享计算机脱机访问计算机,让Windows7脱机共享访问更安全 -电脑资料
  8. ZigBee协议栈解析
  9. 滞后问题_富锂正极材料的电压滞后问题
  10. c语言二进制强制转十六进制,C语言二进制转十六进制问题
  11. Visual Studio Code下载安装教程
  12. Laravel将Word文档转化为pdf文件
  13. 多款比较好用又免费的设计工具
  14. h0215.闭区间问题
  15. 小麦苗微信公众号文章链接地址
  16. JS字符串过滤数字_过滤大写数字
  17. 超超经典语录、看的我心拔凉拔凉的、我真的hold不住!!
  18. Postman使用newman命令执行
  19. ES6-ES11笔记(1)
  20. 【转载】一些著名学习方法

热门文章

  1. 多线程|pi2.c 使用N个线程根据莱布尼兹级数计算PI
  2. 【解决方案】根据当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内
  3. 柴达木盆地第四大气区探实 尖北产量超亿方
  4. 2022-08-04 Brighthouse: An Analytic DataWarehouse for Ad-hoc Queries
  5. Linux操作系统应用实例_Discuz安装
  6. 微信中网页分享开发遇到的坑
  7. 分布式与云计算系统 考试内容总结
  8. 索尼WH-1000XM5什么时候发布 索尼WH-1000XM5配置怎么样
  9. Codeforces Round #702 (Div. 3)ABCEF
  10. 多标签分类算法的研究进展