本文为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. @PrePersist

    @PrePersistpublic void prePersist() { updatedAt = new Timestamp(System.currentTimeMillis()); created ...

  2. ASP.NET Web Api OwinSelfHost Restful 使用

    一.前言 总结一下什么是RESTful架构: (1)每一个URI代表一种资源: (2)客户端和服务器之间,传递这种资源的某种表现层: (3)客户端通过四个HTTP动词,对服务器端资源进行操作,实现&q ...

  3. Token机制,防止web页面重复提交

    1.业务要求:页面的数据只能被点击提交一次 2.发生原因: 由于重复点击或者网络重发,或者nginx重发等情况会导致数据被重复提交 3.解决办法: 集群环境:采用token加redis(redis单线 ...

  4. DedeCMS栏目页调用当前栏目名和上级栏目名

    在构建网页的时候,如果不想逐个写栏目列表页的标题,即列表页标题形式为:{field:seotitle/}_{dede:global.cfg_webname/},其中{field:seotitle/}为 ...

  5. string类总结

    头文件: <string> 初始化: string str(s1); string str("value"); , 'c'); 读写 //输入未知数目的string对象 ...

  6. TypeScript学习笔记(四) - 类和接口

    本篇将介绍TypeScript里的类和接口. 与其他强类型语言类似,TypeScript遵循ECMAScript 2015标准,支持class类型,同时也增加支持interface类型. 一.类(cl ...

  7. linux如何连接移动硬盘

    下载第三方插件的地方: http://www.tuxera.com/community/open-source-ntfs-3g/ 这是具体教程: http://hellopyl.blog.51cto. ...

  8. TestNG入门到...

    目录 一.概述 二.@Test注解常用参数 三.测试中常用的断言(assert) 四.TestNG常用注解及使用 五.配置文件xml常用标签 六.参数传递 七.测试报告 一.概述 1.TestNG是一 ...

  9. linux查找文件或目录命令

    inux查找文件或目录命令,前提:知道文件或者目录的具体名字,例如:sphinx.conf find 查找  find / -name dirname  查找目录 find -name filenam ...

  10. DSP已经英雄迟暮了吗?FPGA才是未来的大杀器?

          DSP技术,在某些人看来,或者已经面临着英雄迟暮的感觉,就我们当前所知道的.Freesacle.ADI.NXP早就停掉了新技术发展,而当前从大的方面说只剩下TI一家扛着Digital Si ...