本文为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的更多相关文章

  1. [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 ...

  2. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  3. 【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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. Leetcode 之Populating Next Right Pointers in Each Node II(51)

    void connect(TreeLinkNode *root) { while (root) { //每一层循环时重新初始化 TreeLinkNode *prev = nullptr; TreeLi ...

  8. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

  9. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

随机推荐

  1. Go 的package

    一.包的一些基本的概念 1.在同一个目录下的所有go文件中,只能有一个main函数.如果存在多个main函数,则在编译的时候会报错 那么,在同一个目录下的两个go文件究竟是什么关系? 为什么会彼此影响 ...

  2. liblinear和libsvm区别

    来源于知乎: 1. LibLinear是线性核,LibSVM可以扩展到非线性核(当也能用线性核,但同样在线性核条件下会比LibLinear慢很多).2. 多分类:LibLinear是one vs al ...

  3. 两行代码搞定js对象深浅拷贝

    有一段时间没有更新博客了,忙于工作.2018年刚过去,今天来开启2018第一篇博文.好了,咱们步入正题. 先上代码 /** * 遍历对象 * 1.判断是不是原始值 * 2.判断是数组还是对象 * 3. ...

  4. type Iterator does not take parameters

    在ubuntu编译java程序时报错:type Iterator does not take parameters 源码如下: package object; import java.util.*; ...

  5. opencv(2)绘图

    绘制直线 函数为:cv2.line(img,Point pt1,Point pt2,color,thickness=1,line_type=8 shift=0) 有值的代表有默认值,不用给也行.可以看 ...

  6. 多线程 or I/O复用select/epoll

    1:多线程模型适用于处理短连接,且连接的打开关闭非常频繁的情形,但不适合处理长连接.线程模型默认情况下,在Linux下每个线程会开8M的栈空间,在TCP长连接的情况下,以2000/分钟的请求为例,几乎 ...

  7. C++中memcpy和memmove

    二者都是内存拷贝 memcpy内存拷贝,没有问题;memmove,内存移动?错,如果这样理解的话,那么这篇文章你就必须要好好看看了,memmove还是内存拷贝.那么既然memcpy和memmove二者 ...

  8. forms.ModelForm 与 forms.Form

    1. 首先 两者都是forms里的常用类. 2. 这两个类在应用上是有区别的.一般情况下,如果要将表单中的数据写入数据库或者修改某些记录的值,就要让表单类继承ModelForm; 如果提交表单后 不会 ...

  9. Bootstrap--响应式显示图片信息列表

    HTML布局 <link href="~/Content/StyleSheet1.css" rel="stylesheet" /> <div ...

  10. ECSHOP中 {insert name='ads' id=$ads_id num=$ads_num}含义

    <div class="smallban">        <ul>                <!-- TemplateBeginEditabl ...