【题解】【BT】【Leetcode】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
思路:
这题看似Binary Tree Level Order Traversal,实则因为满二叉树的设定,相比要简单太多,提前计算好每层的节点数就OK
代码:
void connect(TreeLinkNode *root) {
if(root == NULL) return;//考虑自身为空
queue<TreeLinkNode*> nodeQ;
nodeQ.push(root);
int n = ;
int i = ;
while(!nodeQ.empty()){
TreeLinkNode* top = nodeQ.front();
nodeQ.pop();
if(++i != n){
top->next = nodeQ.front();
}else{
n *= ;
i = ;
}
if(top->left != NULL)//考虑左右子树为空(叶子节点)的情况
nodeQ.push(top->left);
if(top->right != NULL)
nodeQ.push(top->right);
}
}
【题解】【BT】【Leetcode】Populating Next Right Pointers in Each Node的更多相关文章
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LeetCode——Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [leetcode]Populating Next Right Pointers in Each Node II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- LEETCODE —— Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- LeetCode - Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- LeetCode: Populating Next Right Pointers in Each Node 解题报告
Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode { Tree ...
- [LeetCode] [LeetCode] Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
随机推荐
- Linux的五个查找命令(find、locate、whereis、which、type)
1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...
- CentOS linux 下eclipse+cdt编译报undefined reffrece to *
- js打印数组查看
alert() 是不能查看数组,对象的console.log(数组变量); 然后你用火狐的friebug 在控制台查看
- form v
<form name="example_form" action="http://google.com" method="POST"& ...
- 基于K2 BPM的航空业核心业务管理解决方案
基于K2 BPM平台的航空业解决方案,专注航空公司运行类.营销类.管理类所有解决方案. 查看完整版,请访问K2官网http://www.k2software.cn/zh-hans/aviation-i ...
- Tasklist and TaskKill
C:\Users\Administrator>tasklist /? TASKLIST [/S system [/U username [/P [password]]]] [/M ...
- Keil 4.0 生成bin文件
在菜单:Flash /configuare falsh tools ... option for Tatget '项目' /User中:在Run User Programs After Build/R ...
- matlab图形句柄属性总结
原文在于雪漫的bloghttp://blog.sina.com.cn/s/blog_4b9b714a0100cce2.html这两天在看句柄式图形方面的东西,以下是我在看书过程中整理的学习笔记,比较详 ...
- JVM-对象的存活与死亡
当Java虚拟机进行垃圾收集的时候,那么它必须要先判断对象,是否还存活,如果存活就不能对它进行回收.所以判断一个对象是否存活是Java虚拟机必须要实现的. 1.对象是否存活 1)引用计数器:给对象添加 ...
- 分布式Nginx缓存清理(PHP的socket编程)
最近,公司要使用康乐的几台自建CDN换成Nginx,在缓存配置上不会有很多的问题,纠结的问题是:Nginx的如何批量进行缓存清理 我们都知道Nginx提供了一个第三方的模块"nginx ng ...