LeetCode OJ 117. Populating Next Right Pointers in Each Node II
题目
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.
Recursive approach is fine, implicit stack space does not count as extra space for this problem.
Example:
Given the following binary tree,
1
/ \
2 3
/ \ \
4 5 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
解答
这题就是层序遍历二叉树。。。又是一遍就AC了。
下面是AC的代码:
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
queue<TreeLinkNode *> q;
void connect(TreeLinkNode *root) {
if(root == NULL){
return ;
}
q.push(root);
int i;
while(i = q.size()){
while(i--){
TreeLinkNode *temp = q.front();
q.pop();
if(i == 0){
temp->next = NULL;
}
else{
temp->next = q.front();
}
if(temp->left != NULL){
q.push(temp->left);
}
if(temp->right != NULL){
q.push(temp->right);
}
}
}
}
};
137
LeetCode OJ 117. Populating Next Right Pointers in Each Node II的更多相关文章
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...
- LeetCode OJ: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 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/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...
- Java for LeetCode 117 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 堆溢出原理分析
堆溢出与堆的内存布局有关,要搞明白堆溢出,首先要清楚的是malloc()分配的堆内存布局是什么样子,free()操作后又变成什么样子. 解决第一个问题:通过malloc()分配的堆内存,如何布局? 上 ...
- tcpkill,干掉tcp连接
场景 当我们需要在不重启服务的情况下断开某一个TCP长连接时,tcpkill工具就非常有用.比如我们要测试某个长连接断开后程序自动重连的情况. tcpkill安装 这个连接的作者改了一下tcpkill ...
- Codeforces1062C. Banh-mi(贪心+快速幂)
题目链接:传送门 题目: C. Banh-mi time limit per test second memory limit per test megabytes input standard in ...
- 《Linux内核原理与分析》第五周作业
课本:第4章 系统调用的三层机制(上) -用户态.内核态和中断 -用户态:在低的执行级别下,代码能够掌控的范围有所限制,只能访问部分内存. -内核态:在高的执行级别下,代码可以执行特权指令,访问任意的 ...
- js post下载相当于 location.href
/** *参数说明: url:下载地址,val:需要提交的参数值,具体类型和个数自行扩展 * 参数可以用obj = {url:""",val1:"111&quo ...
- 学习笔记TF025:自编码器
传统机器学习依赖良好的特征工程.深度学习解决有效特征难人工提取问题.无监督学习,不需要标注数据,学习数据内容组织形式,提取频繁出现特征,逐层抽象,从简单到复杂,从微观到宏观. 稀疏编码(Sparse ...
- mac下pycharm快捷键
[转载]https://www.cnblogs.com/leolichao/p/9329685.html Mac键盘符号和修饰键说明 ⌘ Command ⇧ Shift ⌥ Option ⌃ Cont ...
- POJ2376 Cleaning Shifts
题意 POJ2376 Cleaning Shifts 0x50「动态规划」例题 http://bailian.openjudge.cn/practice/2376 总时间限制: 1000ms 内存限制 ...
- PythonStudy——魔法函数 Magic Methods
魔法函数 python中以双下划线开始和结束的函数(不可自己定义)为魔法函数 调用类实例化的对象的方法时自动调用魔法函数(感觉不需要显示调用的函数都叫) 在自己定义的类中,可以实现之前的内置函数,比如 ...
- HTTP进阶学习笔记
代理 HTTP的代理服务器既是Web服务器,又是Web客户端.使用代理可以"接触"到所有流过的HTTP流量,代理可以对其进行监视和修改.常见的就是对儿童过滤一些"成人&q ...