[LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路
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 解决思路的更多相关文章
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [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 ...
- leetcode 116 Populating Next Right Pointers in Each Node ----- java
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- Java for LeetCode 116 Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [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 ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
随机推荐
- Appcelerator Titanium 3.x Win7 64位平台安装步骤
刚接触Android移动开发,第一次下载Titanium,第一次下载ADT,第一次看Javascript代码,N多第一次...... 慢慢摸索了一个礼拜把移动开发的工具链的配置学习了一下,抛砖引玉,但 ...
- win7-64bit 下oracle11g plsql 的正确安装
本人在PC机上安装了Oracle 11g 版本号的数据库服务,通过PL/SQL连接数据库时总是无法连接,位的oracle客户端工具instantclient进行转换,另外一种plsql的不能安装到文件 ...
- Jquery UI的datepicker插件使用
原文链接;http://www.ido321.com/375.html Jquery UI是一个非常丰富的Jquery插件,而且UI的各部分插件能够独自分离出来使用.这是其它非常多Jquery插件没有 ...
- C语言原码反码补码与位运算.
目录: 一.机器数和真值 二.原码,反码和补码的基础概念 三.为什么要使用原码,反码和补码 四.原码,补码,反码再深入 五.数据溢出测试 六.位运算 ...
- 面试题 HashMap 原理
HashMap与HashTable的区别 总结: HashMap是用来代替HashTable的类,一般建议使用HashMap.最核心的区别:HashTable的方法是同步的(线程安全),而HashMa ...
- C#Linq的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- call和apply区别
call和apply 基本上是一个意思 区别在于call的第二个参数可以是任意的类型,而apply的第二个参数必须是数组,也可以是arguments.call方法:语法:call(thisObj,Ob ...
- 织梦DEDECMS {dede:field name='position'/}标签增加其它属性的
在默认情况下,织梦(DedeCms)系统当前位置的调用标签为: {dede:field name='position'/} 在这种默认的情况下,生成后的代码大致为如下格式: 主页 > 应用软件 ...
- C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)
1.使用ZXint.Net生成带logo的二维码 /// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name ...
- CodeSMART for VS.NET插件工具
今天无聊,想起以前看过的微软的Visual Studio的插件,所以就找了找. 微软的Visual Studio本身就非常强大了,但是仍然有不足的地方,比如下面要介绍的我喜欢的代码格式化功能的这个插件 ...