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. 小学生之深入C#

    一.深入C#数据类型 值类型传递和引用类型传递 方法的参数是值类型和引用类型 注意:值传递和引用传递判定依据是有没有ref 01.如果方法的参数类型本身就是引用类型,那么对参数值的修改会永久保存 例如 ...

  2. 阿里巴巴iconfont使用方式

    IconFont的作用就是用字体的格式来取代图片.特殊字体的展示,用得比较多的就是一些纯色的图标,具体主要由当前css3属性里的自定义字体(@font-face)来实现. 1.首先在Iconfont- ...

  3. springMVC如何判断入参是默认参数还是请求传过来的参数?

    springMVC如何判断入参是默认参数还是请求传过来的参数?

  4. sqlserver中的统计语法

    set statisitcs io {on | off} 显示与执行的sql语句有关的磁盘活动量的信息 set statistics profile {on | off} 显示语句的配置文件信息 se ...

  5. Andoid源码 BUG修改集合--不断更新

    BUG001:很抱歉,***已停止运行 网上查找问题原因很多,有人说事缓存不够,作为一个开发者,需要从代码解决问题 比如,这次遇到一个"很抱歉,instant已停止运行",inst ...

  6. Android中滑动关闭Activity

    继承SwipeBackActivity即可实现向右滑动删除Activity效果 点击下载所需文件

  7. C# 连接SQL数据库

    感觉很有必要总结一下 一:C# 连接SQL数据库 Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;P ...

  8. 定义Foo() 函数,弹出对话框提示当前选中的是第几个单选框

    function foo(){ var ele = document.getElementsByName("radioElement"); for(var i = 0;i<e ...

  9. PHP Libxml

    PHP Libxml 函数 PHP:指示支持该函数的最早的 PHP 版本. 函数 描述 PHP libxml_clear_errors() 清空 Libxml 错误缓冲. 5 libxml_get_e ...

  10. 软件测试 homework1

    申明数组变量后,在使用的时候,出现了向上溢出的情况(程序运行过程中出现的),导致最后答案不正确,经过输出数组数据发现错误, 现在在申明数组的时候都会大致估算一下,确认申明什么样的数组不会导致溢出. 在 ...