leetcode1104. Path In Zigzag Labelled Binary Tree
题目链接
这个题很容易首先联想到完全二叉树的性质,父结点下标i,如果有左儿子,左儿子下标2i,如果有右儿子,右儿子下标2i+1,相应的一个结点的父结点就是i/2下取整。但是因为这个题在偶数行是从右到左进行计数,因此找一个结点的父结点,就要按照他上一层的排列顺序给他重新找到他的标号。
首先知道label,可以知道它所在的深度depth为⌊log2(label)⌋+1⌊log_2(label)⌋+1⌊log2(label)⌋+1,那么这一层最小的编号是m=2(depth−1)m=2^{(depth-1)}m=2(depth−1),最大的编号是M=2depth−1M=2^{depth}-1M=2depth−1, 通过观察可以得到按照上一层的规律,当前的标号tmp要变成 tmp′=m+M−tmptmp'=m+M-tmptmp′=m+M−tmp ,这样tmp′/2tmp'/2tmp′/2 就是tmp的父结点标号,将tmp更新为tmp’, 继续寻找直到tmp=1为止即可。
做本题踩的坑有:
1.还是不要用log10(level)/log10(2)log_{10}(level)/log_{10}(2)log10(level)/log10(2)来求log2levellog_2{level}log2level, 这样算很容易出玄学bug,因为算出来可能是取整×取整,然后就炸了。可以手写:
以下代码参考
int log(int x) {int cnt = -1; while(x) {x >>= 1; cnt++; }return cnt;
}
如果要追求快速计算的话,可以写:
int log(int n) { int result = 0; if(n&0xffff0000) {result += 16; n >>= 16; } if(n&0x0000ff00) {result += 8; n >>= 8; } if(n&0x000000f0) {result += 4; n >>= 4; } if(n&0x0000000c) {result += 2; n >>= 2; } if(n&0x00000002) {result += 1; n >>= 1; } return result;
}
2.反转vector可以用reverse函数 参考链接
使用头文件**#include < algorithm>**
reverse函数用于反转在**[first,last)**范围内的顺序(包括first指向的元素,不包括last指向的元素),reverse函数没有返回值
注意是前闭后开区间,vector的end()函数指向的是最后一个元素的下一个位置,所以直接传递v.end()就可,使用迭代器访问vector的最后一个元素:参考链接
iter2 = v1.end()-1; //注意v1.end()指向的是最后一个元素的下一个位置,所以访问最后一个元素的正确操作为:v1.end() - 1;
cout << *iter2 << endl;
函数原型:
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{while ((first!=last)&&(first!=--last)){std::iter_swap (first,last);++first;}
}
·
·
·
·
·
AC代码:
class Solution {public:vector<int> pathInZigZagTree(int label) {vector<int> res;int tmp=label;res.push_back(tmp);while(tmp!=1){int level=llog2(tmp)+1;int m=(int)pow(2,(level-1)),M=(int)pow(2,level)-1;tmp=((m+M)-tmp)/2;res.push_back(tmp);}reverse(res.begin(), res.end());//学会调用轮子/*int len=res.size();for(int i=0;i<len/2;i++){int tmp=res[i];res[i]=res[len-i-1];res[len-i-1]=tmp;}*/return res;}int llog2(int x) {int cnt = -1; while(x) {x = x >> 1; cnt++; }return cnt; }
};
leetcode1104. Path In Zigzag Labelled Binary Tree相关推荐
- LeetCode-1104. Path In Zigzag Labelled Binary Tree
LeetCode-1104. Path In Zigzag Labelled Binary Tree 题目描述:https://leetcode.com/problems/path-in-zigzag ...
- 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解题报告
1104. Path In Zigzag Labelled Binary Tree Path In Zigzag Labelled Binary Tree python solution 题目描述 I ...
- LeetCode——1104. 二叉树寻路(Path In Zigzag Labelled Binary Tree)[中等]——分析及代码(Java)
LeetCode--1104. 二叉树寻路[Path In Zigzag Labelled Binary Tree][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 按位置求解 (1 ...
- [Swift]LeetCode1104. 二叉树寻路 | Path In Zigzag Labelled Binary Tree
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 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
题目如下: In an infinite binary tree where every node has two children, the nodes are labelled in row or ...
- 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 ...
- Path In Zigzag Labelled Binary Tree(C++二叉树寻路)
解题思路: (1)从下往上,除2,注意左右顺序 class Solution { private:vector<int> v; public:void parent(int label) ...
最新文章
- 他24岁,4篇Nature在手,也会关心学不懂C语言怎么办
- pandas使用extract函数根据正则表达式从dataframe指定数据列的字符串中抽取出数字(设置expand=false之后返回的为series)、将series转化为dataframe
- 在React 组件中使用Echarts
- Viewport3D 类Viewport3D 类Viewport3D 类
- MPI多机器实现并行计算
- php cile,PHP: Kurulum - Manual
- 建议手机电池85%以下去换电池
- WPF中改进自定义Command一些想法
- 学习Caffe(一)使用Caffe
- python 安装包时出现:SyntaxError: invalid syntax
- 学习Linux的第七十一天
- HSC-1th misc——DORAEMON
- 2016 工作、生活与得失
- 不使用redis,在前后端分离项目的条件下将验证码进行储存
- 安装好office套件以后,右键新建中没有Word、Excel、PPT等怎么办
- display:grid 布局实现两行两列
- Windows7自带截图工具没法保存
- 使用伪类来实现类似微信群聊的头像样式
- 【Python项目】Flask + MySQL 实现用户注册,登录、注销
- 如何扩展Orchard
热门文章
- [Swift] 数组恒等 === 的 bug?!
- html字体大小、颜色、粗体、下划线代码(局部)
- 2021.1.25-2021.1.31
- zuk z2 android 7.0,联想ZUK Z2兑现承诺,即将推出安卓7.0更新
- DBA到底要做什么?
- 【读书笔记】关于《简读中国史》的思考
- 16 Three.js使用dat.GUI简化试验流程
- 零基础重庆自考本科行政管理难吗?
- 乡村少年宫计算机活动简报,乡村学校少年宫活动简讯.doc
- 一文读懂运放共模抑制比(上)