[Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/description/
Description
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note: You may only use constant extra space.
Example
Given the following binary tree,
1
/ \
2 3
/ \ \
4 5 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
Solution
class Solution {
private:
void connectNode(vector<TreeLinkNode*>& v) {
int size = v.size();
for (int i = 0; i <= size - 2; i++) {
v[i] -> next = v[i + 1];
}
}
struct LevelNode {
TreeLinkNode* node;
int level;
LevelNode(TreeLinkNode* n, int l) {
node = n;
level = l;
}
};
const int InitLevel = 1;
public:
void connect(TreeLinkNode *root) {
if (root == NULL)
return;
int curLevel = InitLevel;
vector<TreeLinkNode*> v;
queue<LevelNode> q;
q.push(LevelNode(root, InitLevel));
while (!q.empty()) {
LevelNode ln = q.front();
q.pop();
if (ln.node -> left != NULL) {
q.push(LevelNode(ln.node -> left, ln.level + 1));
}
if (ln.node -> right != NULL) {
q.push(LevelNode(ln.node -> right, ln.level + 1));
}
if (ln.level != curLevel) {
connectNode(v);
v.clear();
curLevel++;
}
v.push_back(ln.node);
}
connectNode(v);
}
};
解题描述
这道题是Populating Next Right Pointers in Each Node的升级版:这道题里面的二叉树不是完全二叉树,所以每一层的节点数目没有办法提前计算。我想到的算法是,使用一个新的结构体来记录队列中的节点,然后这个结构体中包含另外一个属性——即层次编号。通过层次编号来区分不同层次的节点,在层次编号发生变化的时候对当前层次记录表中的节点进行next连接即可。
[Leetcode Week15]Populating Next Right Pointers in Each Node II的更多相关文章
- [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- 【leetcode】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [Leetcode][JAVA] Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- leetcode 117 Populating Next Right Pointers in Each Node II ----- java
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- Leetcode 之Populating Next Right Pointers in Each Node II(51)
void connect(TreeLinkNode *root) { while (root) { //每一层循环时重新初始化 TreeLinkNode *prev = nullptr; TreeLi ...
- Leetcode#117 Populating Next Right Pointers in Each Node II
原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...
随机推荐
- windows网络模型
Windows提供了四种异步IO技术,机制几乎时相同的,区别在于通知结果的方式不同: 1.通过注册的消息函数进行通知 2.通过内核event事件进行通知 3.通过称为完成例程的回调函数进行通知 4.通 ...
- 好记性不如烂笔头之Maven使用小记
一.前言 说起Maven,是在我上上东家接触的,掌握的还不错,因为种种原因,上家公司没有使用太多大众技术,我也没有太多施展的机会,对于以前掌握的技术,很多都荒废了,最近使用起来发现有点儿吃力了,为了加 ...
- 第54天:原生js实现轮播图效果
一.轮播图的原理: 一系列的大小相等的图片平铺,利用CSS布局只显示一张图片,其余隐藏.通过计算偏移量利用定时器实现自动播放,或通过手动点击事件切换图片. 二.Html布局 首先父容器containe ...
- usebean 使用语法
- zoj3209-Treasure Map
给出一个左下角为\((0,0)\),右上角为\((n,m)\)的矩形,再给出\(k\)个在大矩形内的小矩形(可以重合),问是否能用这些小矩形完全覆盖这个大矩形,并且没有重合,如果可以至少需要多少个. ...
- Python面向对象—类属性和实例属性
属性:就是属于一个对象的数据或函数元素 类有类方法.实例方法.静态方法.类数据属性(类变量)和实例数据属性(实例变量). 类属性:包括类方法和类变量,可以通过类或实例来访问,只能通过类来修改. 实例属 ...
- html dom与javascript的关系 -我们用JavaScript对网页(HTML)进行的所有操作都是通过DOM进行的
一,什么是DOM (参考源http://www.cnblogs.com/chaogex/p/3959723.html) DOM是什么 DOM全称为The Document Object Model,应 ...
- AD高级培训PPT总结
AD高级培训PPT总结 https://bbs.sangfor.com.cn/forum.php?mod=viewthread&tid=44905&highlight= 说明: ...
- BZOJ3495 PA2010 Riddle 【2-sat】
题目链接 BZOJ3495 题解 每个城市都有选和不选两种情况,很容易考虑到2-sat 边的限制就很好设置了,主要是每个郡只有一个首都的限制 我们不可能两两之间连边,这样复杂度就爆炸了 于是乎就有了一 ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...