Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
Populating Next Right Pointers in Each Node II
Total Accepted: 9695 Total
Submissions: 32965
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.
For 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
题意:给定一棵随意二叉树(不一定是perfect binary tree),将它每个节点的next指针都指向该节点右边的节点
思路:bfs
这里不能用dfs了,仅仅能用bfs
bfs遍历将同一层的节点存放在同一个数组里,
然后在遍历每一个数组,将前面的节点和后面的节点connect起来,
最后一个节点和NULL connect起来
须要定义一个新的struct结构,保存指向每一个节点的指针和该节点所在的层
复杂度:时间O(n), 空间O( n)
相关题目:
Populating Next Right Pointers in Each Node
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public: struct TreeLinkNodeWithLevel
{
TreeLinkNode *p;
int level;
TreeLinkNodeWithLevel(TreeLinkNode *pp, int l):p(pp), level(l){}
}; void connect(TreeLinkNode *root) {
vector<vector<TreeLinkNode *> > nodes;
queue<TreeLinkNodeWithLevel> q;
q.push(TreeLinkNodeWithLevel(root, 0));
while (!q.empty())
{
TreeLinkNodeWithLevel cur = q.front(); q.pop();
if(!cur.p) continue;
if(cur.level >= nodes.size()){
vector<TreeLinkNode *> temp;
temp.push_back(cur.p);
nodes.push_back(temp);
}else{
nodes[cur.level].push_back(cur.p);
}
TreeLinkNodeWithLevel left(cur.p->left, cur.level + 1);
TreeLinkNodeWithLevel right(cur.p->right, cur.level + 1);
q.push(left);
q.push(right);
}
for(int i = 0; i < nodes.size(); i++){
for(int j = 0; j < nodes[i].size() - 1; j++){
nodes[i][j]->next = nodes[i][j + 1];
}
nodes[i][nodes[i].size() - 1]->next = NULL;
}
}
};
Leetcode 树 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 Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- 【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][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 ...
- 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 之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< ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
随机推荐
- Python subprocess- call、check_call、check_output
简介 subprocess模块用来创建新的进程,连接到其stdin.stdout.stderr管道并获取它们的返回码.subprocess模块的出现是为了替代如下旧模块及函数:os.system.os ...
- Python_oldboy_自动化运维之路(四)
本节内容 集合 字符编码与转码 函数语法及基本特性 函数参数与局部变量 返回值和嵌套函数 递归 匿名函数 高阶函数 1.集合 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变 ...
- java基础70 负责静态的网页制作语言XML(网页知识)
HTML:负责网页结构的CSS:负责网页的样式(美观)JavaScript:负责客户(浏览器)端与用户进行交互 1.HTML语言的特点 1.由标签组成 2.语法结构松散 3.大小写不区分 ...
- wpf image blur
RenderOptions.BitmapScalingMode="NearestNeighbor"
- XShell安装
Xshell就是一个远程控制Centos的软件:(用XShell比较方便,试用的都知道,界面也人性化) 详细介绍请看 百度百科 下面我们来安装下这个工具: 双击exe 点下一步: 选 免费的 然后下一 ...
- Linux学习笔记:ps -ef、ps aux、kill -9
一.查看进程命令 1.ps命令 Linux中的ps命令是Process Status的缩写. ps命令用来列出系统中当前运行的那些进程. ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻 ...
- Sourcetree使用 - git图形化工具(三)
前面两个章节总结了Sourcetree的安装与配置Sourcetree密钥,这个章节主要讲如何使用Sourcetree.以前呢,都是使用git Bash进行命令行方式进行操作git,感觉部分时间浪费在 ...
- Spark(一)Spark简介
一.官网介绍 1 什么是Spark 官网地址:http://spark.apache.org/ Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎.Spark是UC Berkel ...
- vue 笔记备份
Vue实现数据双向绑定的原理:Object.defineProperty() vue实现数据双向绑定主要是:采用数据劫持结合发布者-订阅者模式的方式,通过Object.defineProperty() ...
- HTTP上传大文件的节点配置
<system.web> <compilation debug="true" targetFramework="4.0" /> < ...