题目

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,



After calling your function, the tree should look like:

分析

为满二叉树添加线索;

由题目可知,线索是根据层序链接,每一层终止节点线索为空,其余节点的next为其层序的下一个节点;

利用层序遍历解决该问题;类似于LeetCode(102) Binary Tree Level Order Traversal

AC代码

/**
* 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)
return; //定义两个队列,一个存储父节点
queue<TreeLinkNode *> parent;
parent.push(root);
root->next = NULL;
while (!parent.empty())
{
//定义队列存储当前子层
queue<TreeLinkNode*> curLevel;
while (!parent.empty())
{
TreeLinkNode *tmp = parent.front();
parent.pop();
if (tmp->left)
curLevel.push(tmp->left);
if (tmp->right)
curLevel.push(tmp->right); }//while
parent = curLevel; while (!curLevel.empty())
{
TreeLinkNode *p = curLevel.front();
curLevel.pop();
if (!curLevel.empty())
p->next = curLevel.front();
else
p->next = NULL;
}//while
}//while
}
};

GitHub测试程序源码

LeetCode(116) Populating Next Right Pointers in Each Node的更多相关文章

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

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

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

  4. LeetCode(116):填充同一层的兄弟节点

    Medium! 题目描述: 给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *n ...

  5. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  6. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  7. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  8. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

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

  9. LeetCode OJ: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 ...

随机推荐

  1. 牛客寒假6-E.海啸

    链接:https://ac.nowcoder.com/acm/contest/332/E 题意: 有一个沿海地区,可以看作有n行m列的城市,第i行第j列的城市海拔为h[i][j]. 由于沿海,所以这个 ...

  2. [WOJ4354] 蜀石经

    题目链接: 点我 题目分析: 大模拟,貌似\(O(n^2)\)也可以卡常过,复杂度正确的做法是用优先队列维护. 代码: #include<bits/stdc++.h> #define N ...

  3. SNMP消息传输机制

    1.引言 基于TCP/IP的网络管理包含3个组成部分: 1) 一个管理信息库MIB(Management Information Base).管理信息库包含所有代理进程的所有可被查询和修改的参数.RF ...

  4. Lock简介

    digest synchronized已经提供了锁的功能,而且还是Java的内置特性,那为什么还要出现lock呢? 用一句话来讲就是——synchronized可以实现同步,但太死板了它的同步机制:l ...

  5. mybatis(错误) 项目启动时报“Result Maps collection already contains value forxxx”的解决方案

    使用逆向工程生成代码时,一定要将原来的代码删除干净,如果覆盖的话,不是真正的覆盖,在原来的代码上增加重复的代码,导致出错

  6. jasmine+karma 自动化单元测试

    测试的必须性 相信大家都知道测试的必要性,测试先行的概念.不过,写了这么多年的代码,除了之前用java的时候写过一些测试用例,还真的很少写过前端的测试用例,或者做一些自动化测试.感觉做单元测试还是很有 ...

  7. arcgis mdb 数据中的shp 如何合并一起

    如上操作  一直往下就可以啦 选择数据源 就可以了,然后就可以load  其他数据啦   ,坐标系要一直

  8. dataTable 中数据的居中显示

    遇到了一个小问题,就是在向dataTable中添加数据时,数据总是向左对齐,而dataTable又没有设置数据对齐的方法,这里写一个在网上看到的一个方法,分享出来看一下,简单实用. html代码如图1 ...

  9. 数据库查询,显示为树形结构(easyui+SSM)

    在实际项目上,有很多地方后台存了一个表,但是在显示查询的时候需要显示为树形结构. 本项目是easyui+SSM框架. 前台程序为: <!DOCTYPE html> <html> ...

  10. 关于h5中背景音乐的自动播放

    音乐的自动播放属性,这里也介绍一下: <audio controls="controls" autoplay="autoplay"> <sou ...