LeetCode-1104. Path In Zigzag Labelled Binary Tree
LeetCode-1104. Path In Zigzag Labelled Binary Tree
- 题目描述:https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/
解题思路
- 二叉树的节点全满的话一般都会有些数学规律的,要善于发现数学规律,二叉树层数用
n
表示(层数从0层开始数),每一层的节点数为2^n
(题目表明该树是无限二叉树);要找的数字用label
表示;label
对应所在数组的下标为index
表示;这种题我们分析找规律时要同时画树和对应的数组来结合分析。容易发现,当n
为偶数时,label = index + 1
;当n
为奇数时,index = 2^n - 1 + ( 2^(n+1) - 1 - label) = (2^n) * 3 - label - 2
,label和index的关系确定,剩下的就好办了;对于一棵树,子节点和父节点的关系如图(容易得到父子节点关系:parent = (child - 1) / 2
):
搞清节点与节点之间,数组元素与元素间,下标与下标,下标与要找的数之间的关系,相信编码就不是难事了。
算法描述
- Step1:先找出
label
所在的树的层数n
和对应所在数组的下标index
(其实树和数组都不需要我们真实的构造出来,只是分析解题时借助一下就行),创建存放路径path的list
- Step2:判断
n
的奇偶性,如果是偶数,index+1
即是我们所找的节点元素值,把它放入list
的头部;如果是奇数,所找的节点元素值为:(2^n) * 3 - index - 2
(这是由解题思路中index
和label
之间的关系得来的,此时要注意:这里的label
指的是路径path中每个节点的值,不仅仅是题目所要找的label
) - Step3:
n--
更新n
的值,往上递推,同时也更新index
的值,index = (index - 1)/2
- Step4:循环执行Step2和Step3,直到
n<0
代码实现-Java
class Solution {public List<Integer> pathInZigZagTree(int label) {int n = 0;while(Math.pow(2,n) < label){if(Math.pow(2,n+1) > label){break;}else{n++;}}int indexOfLabel = 0;if(n % 2 == 0){indexOfLabel = label - 1 ;}else{indexOfLabel = (int) (Math.pow(2,n) * 3 - label - 2);}List<Integer> list = new LinkedList<>();while(n >= 0){if(n % 2 == 0){list.add(0,indexOfLabel + 1);}else{list.add(0,(int) (Math.pow(2,n) * 3 - indexOfLabel - 2));}indexOfLabel = (indexOfLabel - 1)/2;n--;}return list;}
}
运行结果性能数据:
Runtime: 0 ms, faster than 100.00% of Java online submissions for Path In Zigzag Labelled Binary Tree.
Memory Usage: 33.4 MB, less than 100.00% of Java online submissions for Path In Zigzag Labelled Binary Tree.
LeetCode-1104. Path In Zigzag Labelled Binary Tree相关推荐
- LeetCode 1104. Path In Zigzag Labelled Binary Tree解题报告
1104. Path In Zigzag Labelled Binary Tree Path In Zigzag Labelled Binary Tree python solution 题目描述 I ...
- 1104. Path In Zigzag Labelled Binary Tree**
1104. Path In Zigzag Labelled Binary Tree** https://leetcode.com/problems/path-in-zigzag-labelled-bi ...
- 【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 ...
- 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 ...
- LeetCode——1104. 二叉树寻路(Path In Zigzag Labelled Binary Tree)[中等]——分析及代码(Java)
LeetCode--1104. 二叉树寻路[Path In Zigzag Labelled Binary Tree][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 按位置求解 (1 ...
- 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 ...
- [Swift]LeetCode1104. 二叉树寻路 | Path In Zigzag Labelled Binary Tree
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- leetcode1104. Path In Zigzag Labelled Binary Tree
题目链接 这个题很容易首先联想到完全二叉树的性质,父结点下标i,如果有左儿子,左儿子下标2i,如果有右儿子,右儿子下标2i+1,相应的一个结点的父结点就是i/2下取整.但是因为这个题在偶数行是从右到左 ...
- Path In Zigzag Labelled Binary Tree(C++二叉树寻路)
解题思路: (1)从下往上,除2,注意左右顺序 class Solution { private:vector<int> v; public:void parent(int label) ...
最新文章
- 偏度与峰度的正态性分布判断
- 移动硬盘格式化了的资料找回方法
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
- SGS 0.9.7 协议解析
- 为什么我的对象被 IntelliJ IDEA 悄悄修改了?
- Java8对List<Integer>的求和
- IT综合学习网站收集
- 计算机系统的备份与还原实验报告,数据库《数据备份与还原》实验报告.doc
- Vue学习笔记(九) Vue CLI
- 从FTP入侵到SQL
- 百度API获取位置范围内的周边服务
- Proxmark3教程1:小白如何用PM3破解复制M1全加密门禁IC卡
- 思科决定将不修复路由器中的这70多个漏洞
- typescript 第一弹
- 64位ODBC数据源配置
- Snipaste操作指南
- ​手把手教你做个AR涂涂乐 ​
- 60mph和kmh换算_mph换算器(速度计算器在线)
- 为什么RGB 与 CMYK的差异,会有所不同?
- RLC交流电路测量实验