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

问题: 给定一个二叉树,将树元素的 *next 指向该元素在树结构中的水平右边节点。

这是广度遍历的一个应用。可以借组队列结构实现广度遍历,求解题目。

思路:

  • 当队列中元素恰好是树种某一行的全部元素时,则给队列中每个节点的 *next 赋值为 列表中对应的下一个节点,其中特别地,最后一个元素*next 为 NULL。
  • 将队列中的元素全部弹出,并依次塞进他们的子节点,此时,队列中的元素恰好是树种下一行的全部元素,继续上一步操作。

将根节点塞进队列,即实现了上面思路的初始化。

     void connect(TreeLinkNode *root) {

         if(root == NULL){
return;
} list<TreeLinkNode*> queue; queue.push_back(root); while(queue.size() > ){ // assign value to the next point of the node in queue.
list<TreeLinkNode*>::iterator q_iter;
for( q_iter = queue.begin() ; std::next(q_iter,) != queue.end(); q_iter++){
(*q_iter)->next = *std::next(q_iter,);
} // pop each node in the current row in the tree structure, and push the left and right childrens of them into queue.
while(queue.front()->next != NULL){
TreeLinkNode* node = queue.front();
queue.pop_front(); if(node->left != NULL){
queue.push_back(node->left);
} if(node->right != NULL){
queue.push_back(node->right);
}
} TreeLinkNode* node = queue.front();
queue.pop_front(); if(node->left != NULL){
queue.push_back(node->left);
} if(node->right != NULL){
queue.push_back(node->right);
}
}
}

[LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路的更多相关文章

  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 Populating Next Right Pointers in Each Node ----- java

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

  4. Java for LeetCode 116 Populating Next Right Pointers in Each Node

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

  5. leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法

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

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

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

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

  8. 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

    题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...

  9. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

随机推荐

  1. Debian编译内核

    转自 yuzibo博客 http://yuzibo.github.io/DebianBuildKernel.html 最终成功一次了 之前又一次编译了好多次.可惜没有一次成功的,说实话.借助Debia ...

  2. Java实现BASE64编解码

    Java实现BASE64编解码 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs BASE64和其它类似的编码算法通经常使用于转换二进制数据为文本数据,其目 ...

  3. uploadify上传大文件时出现404错误

    出现这个错误的话一般是IIs限制了文件大小.IIS7下的默认设置限制了上传大小.这个时候Web.Config中的大小设置也就失效了.具体步骤:1.打开IIS管理器,找到Default Web Site ...

  4. js window.open()弹出窗口参数说明及居中设置

    window.open()可以弹出一个新的窗口,并且通过参数控制窗口的各项属性. 最基本的弹出窗口代码 window.open('httP://codeo.cn/'); window.open()各参 ...

  5. js中的for...in循环机制

    1) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.o ...

  6. html:标签原本属性

    <!doctype html>无标题文档 a标签,默认有text-decoration属性 span标签不需要清零 b标签不需要清零 em标签不需要清零 strong 相邻内嵌元素代码里面 ...

  7. hdu 2111

    #include <iostream> #include <algorithm> using namespace std; struct money { int s; int ...

  8. Avi视频生成缩略图时,提示“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”

    需求:录制Avi格式视频成功后,使用DirectShow生成缩略图,由于视频录制时,宽高分辨率可调节,所以有些情况下,生成缩略图会抛出异常“尝试读取或写入受保护的内存.这通常指示其他内存已损坏”. 异 ...

  9. sql server 导出

    http://ssat.codeplex.com/SourceControl/latest 用于连接sql server

  10. 华为S5300交换机配置基于VLAN的本地端口镜像

    配置思路 1.  将Ethernet0/0/20接口配置为观察端口(监控端口) 2.  将VLAN 1.11.12.13.14配置为镜像VLAN 配置步骤 1.  配置观察端口 <Switch& ...