[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< ...
随机推荐
- Matlab 函数ndims简介,flipdim简介
ndims是matlab中求一个数组维数的函数. 调用格式: n=ndims(A) 将A的维数返回给变量n.n>=2 n=ndims(A)与n=length(size(A))是等价的 MATLA ...
- BZOJ 1509 逃学的小孩(树的直径)
题意:从树上任找三点u,v,w.使得dis(u,v)+min(dis(u,w),dis(v,w))最大. 有一个结论u,v必是树上直径的两端点. 剩下的枚举w就行了. 具体不会证... # inclu ...
- prototype的本质
在<关于思维方式的思绪>那篇文章里提到了, 原型的本质就是一种委托关系. 即我这里没有,就到我的原型里去看看,一旦找到就当成我的用. 本文详细说一下这个事情. 比如某女买东西,钱都是她老公 ...
- Oracle 获取数据最新版本
表数据如: 以CODE 作为版本分组字段,对创建时间进行按最新排序 ID NAME CODE CREATE_TIME 1ffg abc 001 2014-01-01 1gff ...
- [JSOI2010]部落划分 最小生成树
一道最小生成树经典题 由于是最靠近的两个部落尽可能远,如果我们先处理出任意两个居住点之间的距离并将其当做边,那么我们可以发现,因为在一个部落里面的边是不用计入答案的,所以应该要尽量把小边放在一个部落里 ...
- BZOJ3653 & 洛谷3899:谈笑风生——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3653 https://www.luogu.org/problemnew/show/P3899 设 ...
- C++中typedef和#define简介
本文基于<C++ Primer(第5版)>和网上博客,整理而成. 一.类型别名 类型别名是一个名字,它是某种类型的同义词,有两种方法可用于定义类型别名:typedef.using. 1.关 ...
- React生命周期的变化
1.ES6语法的引入,砍掉了getDefaultProps和getInitialState getDefaultProps 使用 static default={}的方式代替getInitialSta ...
- 阿里云配置redis
一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sor ...
- 实例——简单的Samba共享
服务端配置 # 临时停止iptables service iptables stop # 临时禁用SELinux setenforce 0 # 禁止iptables开机自动启动 chkconfig i ...