/**
* 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:
void connect(TreeLinkNode *root) {
if(root == NULL) return;
queue<TreeLinkNode *> que;
que.push(root);
while(!empty(que)){
int lens = que.size();
for(int i=;i < lens-;i++){
TreeLinkNode *temp = que.front();
que.pop();
temp->next = que.front();
if(temp->left) que.push(temp->left);
if(temp->right) que.push(temp->right);
}
TreeLinkNode *temp2 = que.front();
temp2->next = NULL;
que.pop();
if(temp2->left) que.push(temp2->left);
if(temp2->right) que.push(temp2->right);
}
}
};

_

Leetcode 116的更多相关文章

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

  2. [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针

    You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...

  3. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

  4. [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)

    问题 给出如下结构的二叉树: struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...

  5. [LeetCode] 116&117. Populating Next Right Pointers in Each Node I&II_Medium tag: BFS(Dont know why leetcode tag it as DFS...)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  6. Java实现 LeetCode 116 填充每个节点的下一个右侧节点指针

    116. 填充每个节点的下一个右侧节点指针 给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点.二叉树定义如下: struct Node { int val; Node *left ...

  7. [leetcode] 116. 填充同一层的兄弟节点

    116. 填充同一层的兄弟节点 其实就是个二叉树的层次遍历 class Solution { public void connect(TreeLinkNode root) { if (root == ...

  8. leetcode 116 Populating Next Right Pointers in Each Node ----- java

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  9. [LeetCode#116]Fraction to Recurring Decimal

    Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...

  10. [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

随机推荐

  1. Unity3D学习笔记(三十二):Xlua(2)

    Xlua支持通过子类对象访问父类的变量属性和方法   对于C#的ref,out参数的方法 当调用的时候:out类型的参数是不需要传递实参的,普通的参数和ref参数需要传递实参. out,ref传出值通 ...

  2. 在Mac OSX下安装Microsoft Calibri字体

    参考: Where can I find default Microsoft fonts Calibri, Cambria? 在Mac OSX下安装Microsoft Calibri字体 1.下载: ...

  3. 定义统一的返回格式(controller)

    一:单独创建一个类来表示返回结果 package com.jk51.commons.dto; /** * Created by Administrator on 2017/6/13. */ publi ...

  4. Python 爬起数据时 'gbk' codec can't encode character '\xa0' 的问题

    1.被这个问题折腾了一上午终于解决了,再网上看到有用  string.replace(u'\xa0',u' ') 替换成空格的,方法试了没用. 后来发现 要在open的时候加utf-8才解决问题. 以 ...

  5. mark mem

    韦达定理 http://baike.baidu.com/link?url=M45ozZEnQ4BtKD7l22WWgQuGnmDYV7TFynQcPEO2Tt8leYGhyEa1flt-RM34NG4 ...

  6. Mysql 函数使用记录(二)——ELT()、FIELD()、IFNULL()

    昨天在对一业务修改的过程中想到用DECODE()来实现效果,转眼发现目前使用的是Mysql库,经过查阅,最终用ELT().FIELD().IFNULL()函数来实现需求.现对其做一个记录. 语法: E ...

  7. gensim使用方法以及例子

    来自:https://blog.csdn.net/u014595019/article/details/52218249 gensim是一个Python的自然语言处理库,能够将文档根据TF-IDF,L ...

  8. Django表单API详解

    声明:以下的Form.表单等术语都指的的广义的Django表单. Form要么是绑定了数据的,要么是未绑定数据的. 如果是绑定的,那么它能够验证数据,并渲染表单及其数据,然后生成HTML表单.如果未绑 ...

  9. isnull和sum的关系

    这是我刚刚写存储过程的时候意识到的一个问题!!! 先问大家这样一个问题,print 100+null  等于多少? 在一组数据统计的过程中,只要使用到sum函数,就必须使用isnull函数包含起来,因 ...

  10. C#_方法的重载

    方法的重载是一种操作性多态,有的时候,可能需要在多个不同的实现中对不同的数据执行相同的逻辑操作,以writeline方法为例,有时可能想他传递一个整数.两者的具体实现肯定是不同的,但在逻辑上,这个方法 ...