Given a binary tree

    struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,

Given the following perfect binary tree,

         1
/ \
2 3
/ \ / \
4 5 6 7

After calling your function, the tree should look like:

         1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL

思路:

这题看似Binary Tree Level Order Traversal,实则因为满二叉树的设定,相比要简单太多,提前计算好每层的节点数就OK

代码:

 void connect(TreeLinkNode *root) {
if(root == NULL) return;//考虑自身为空
queue<TreeLinkNode*> nodeQ;
nodeQ.push(root);
int n = ;
int i = ;
while(!nodeQ.empty()){
TreeLinkNode* top = nodeQ.front();
nodeQ.pop();
if(++i != n){
top->next = nodeQ.front();
}else{
n *= ;
i = ;
}
if(top->left != NULL)//考虑左右子树为空(叶子节点)的情况
nodeQ.push(top->left);
if(top->right != NULL)
nodeQ.push(top->right);
}
}

【题解】【BT】【Leetcode】Populating Next Right Pointers in Each Node的更多相关文章

  1. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

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

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

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

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

  4. LeetCode——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]Populating Next Right Pointers in Each Node II @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...

  6. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

  7. LEETCODE —— Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  8. LeetCode - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  9. LeetCode: Populating Next Right Pointers in Each Node 解题报告

    Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode {      Tree ...

  10. [LeetCode] [LeetCode] Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

随机推荐

  1. Hadoop作业JVM堆大小设置优化 [转]

    前段时间,公司Hadoop集群整体的负载很高,查了一下原因,发现原来是客户端那边在每一个作业上擅自配置了很大的堆空间,从而导致集群负载很高.下面我就来讲讲怎么来现在客户端那边的JVM堆大小的设置.我们 ...

  2. 利用百度地图开源sdk获取地址信息。

    注册百度开发者帐号,下载相关sdk 添加权限: 添加百度注册访问应用(AK)码 添加源代码文件到libs文件: 代码如下: package com.lixu.baidu_gps; import com ...

  3. ncs安装及初次运行

    Tail-f NCS 作为网络配置程序和基础设备之间的接口,能够展现各种服务,修改各开发商不相同的设备配置,同时能及时同步网络设备状态到cdb(configuration database,配置数据库 ...

  4. ActiveX 技术疑点 一

    1.编写基于MFC Activex 使用 静态库 .lib , MFC 的使用 在静态库中使用MFC . 生成ocx 文件 注册失败.提示: ***还是一个可执行文件,没有为这个文件类型注册的注册帮助 ...

  5. IE11无法 登陆银行网站

    1,打开IE11,看着键盘,按住Alt+X,然后按字母O打开IE设置选项=>[安全]选项卡把安全级别拉到最下,关闭[启用保护模式] 2,点击[受信任的站点]将支付宝和农业银行网址添加进去,关闭选 ...

  6. powershell加win的dns服务器,解决网站负载均衡问题

    用我发明的powershell填坑法,加windows的dns服务器.从调整dns服务器解析ip时间段的角度,解决网站负载均衡问题. ------------------------win2012r2 ...

  7. Android 应用按两下返回键退出应用程序

    在android应用开发中,有时候应用会用到按两下返回键退出应用的功能,今天介绍一下这个功能,直接上代码: @Override public boolean dispatchKeyEvent(KeyE ...

  8. java基础之 异常

    Throwable是所有Java程序中错误处理的父类,有两种资类:Error和Exception. Error:表示由JVM所侦测到的无法预期的错误,由于这是属于JVM层次的严重错误,导致JVM无法继 ...

  9. Ajax get方法 IE 下乱码

    每个浏览器处理编码的格式不同. ajax使用utf-8来编码发送数据,ie在发送时并没加上charset=utf-8,从而导致乱码(IE默认使用iso-8859-1编码) JavaScript代码: ...

  10. 访问FLASH设备-W25X16

    /************************************* *文件名称:w25x16_spi.c * *功能描述:访问和写入数据到闪存w25x16 * *建立日期:2016-03-1 ...