本文为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. WebApi Owin SelfHost OAuth2 - 授权服务和资源服务分离方案

    使用JWT 参考:http://www.cnblogs.com/grissom007/p/6294746.html

  2. docker centos:latest 使用 sshd

    一.术语 1.容器 很多用户在接触Docker 之初都会认为容器就是一种轻量级的虚拟机,但实际上,容器和虚拟机有非常大的区别.从根本形态上来看,容器其实就是运行在操作系统上的一个进程,只不过加入了对资 ...

  3. Python基础 - Ubuntu+Nginx+uwsgi+supervisor部署Flask应用

    网上找了许多讲关于Flask应用部署的文章几乎都是一个helloworld的Demo,按照helloworld来部署都没问题,但实际项目部署时还是遇到了不少问题.在这里简单写下自己成功部署的过程,防止 ...

  4. 如何阻止点击scrollviewer里面的单位内容时,自动滚动

    <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="FocusVisualStyle ...

  5. Codeforces 948C Producing Snow(优先队列+思维)

    题目链接:http://codeforces.com/contest/948/problem/C 题目大意:给定长度n(n<=1e5),第一行v[i]表示表示第i堆雪的体积,第二行t[i]表示第 ...

  6. 题解-python-CodeForces 227A

    codeforces题目,用python写 本题输入三个点坐标,考察叉积,若大于0则right,小于0则left,等于0则towards 代码: a = raw_input().split() b = ...

  7. 矩阵链乘(UVa 442)

    结构体 struct matrix 用来保存矩阵的行和列: map<string,matrix> 用来保存矩阵名和相应的行列数: stack<string> 用来保存表达式中遇 ...

  8. HBase的基础知识

    1.HBase(NoSQL:不是关系型数据库)的逻辑数据模型 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC S ...

  9. 001 Anaconda的介绍与安装

    1.官网 www.continuum.io 2.ananconda的版本 同一个版本下对应一个python3与python2,在这里下载使用python 2.7的版本. 3.概述 Anaconda是一 ...

  10. 纯CSS实现3D图像轮转

    CSS演武场今天继续,今天看一个纯css实现的3D图像轮转效果,请大家猛戳研究效果先,也可下载收藏先. 首先看html文件,div.billboard为效果的容器,利用10个div.poster分割图 ...