[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 ...
随机推荐
- JavaScript高级编程II
原文地址: http://www.onlamp.com/pub/a/onlamp/2007/08/23/advanced-javascript-ii.html?page=1 在前面的文章中, ...
- Java基础知识强化31:String类之String的面试题
1.先看一个图: 2.String面试题: (1)题1: package cn.itcast_02; /* * 看程序写结果 */ public class StringDemo3 { public ...
- VS2010 快捷键--我的总结
启动VS,可在运行中输入“devenv”: [窗口快捷键] Ctrl+Alt+L 解决方案管理器 Ctrl+W,C: 类视图 Ctrl+W,O: 输出视图 Ctrl+W,T: 任务 ...
- mongodb权威指南读书笔记
一个服务器能不能运行多个不同端口的mongo实例? 如果两个对象不相等,hashcode一定不相等:如果两个对象相等,hashcode相等或者不相等? 修改器速度42页 update({},{&quo ...
- Windows 设置时间同步
1.Windows Server 2008 r2 注:{}内是你要同步的外部服务器地址,例如复旦的时间同步服务器地址为:ntp.fudan.edu.cn,则完整命令如下: w32tm /config ...
- Java前端Rsa公钥加密,后端Rsa私钥解密(目前还不支持中文加密解密,其他都行)
Base64工具类,可以让rsa编码的乱码变成一串字符序列 package com.utils; import java.io.ByteArrayInputStream; import java.io ...
- intellij idea 热部署失效,需要手动编译类
从网上看到的解决方案,做一下备忘: spring boot项目中遇到jrebel类需要手动编译才会触发热部署的问题(spring boot devtools一样的问题) 1.ctl + shift + ...
- oracle 存储过程,函数和包
创建存储过程: 语法:create [or replace] PROCEDURE 过程名(参数列表) AS PLSQL子程序体: 调用 存储过程的方式 两种1.execute(exec) - ...
- sql用户权限
登录 1)右键根目录属性 点下面的sql server 和 windows 身份验证模式 2)安全性右键新建,选择登陆 去掉 那个"用户下次登陆是必须改密码" ,下面默认数据库改为 ...
- 你好,C++(23) 4.4.2 工资程序成长记:用数组处理批量数据,用循环结构执行重复动作
4.4 从语句到程序 了解了各种表达式和语句之后,就相当于掌握了写作文要用到的词语和句子,但是,仅有词语和句子是无法构成一篇有意义的文章的.要完成一篇文章,先需要确定这篇文章的结构,是先分述再总述, ...