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 ...
随机推荐
- Windows10 VS2017 C++使用crypto++库加密解密(AES)
参考文章: https://blog.csdn.net/tangcaijun/article/details/42110319 首先下载库: https://www.cryptopp.com/#dow ...
- document.ready(function(){}),window.onload,$(function(){})的区别
https://blog.csdn.net/qkzhx0516/article/details/79236514
- XXS level8
(1)查看PHP源代码 <?php ini_set("display_errors", 0); $str = strtolower($_GET["keyword&q ...
- JVM垃圾回收算法解析
JVM垃圾回收算法解析 标记-清除算法 该算法为最基础的算法.它分为标记和清除两个阶段,首先标记出需要回收的对象,在标记结束后,统一回收.该算法存在两个问题:一是效率问题,标记和清除过程效率都不太高, ...
- Visual Studio 2017 常用快捷键
1.窗口快捷键 Ctrl+W,W: 浏览器窗口 Ctrl+W,S: 解决方案管理器 (Solution) Ctrl+W,C: 类视图 (Class) Ctrl+W,E: 错误列表 (Error) Ct ...
- 为git服务器配置gitosis管理权限
yum install python-setuptools git clone https://github.com/tv42/gitosis.git cd gitosis sudo python s ...
- MySQL Error--The Table is full
问题描述 在MySQL 错误日志中发下以下错误信息:[ERROR] /export/servers/mysql/bin/mysqld: The table '#sql-xxxx-xxx' is ful ...
- 解决Android Studio在Ubuntu上出现“sdk/platform-tools/adb: error=2, No such file or directory”的方法
转载至http://blog.163.com/china_uv/blog/static/11713726720136931132385/ 刚安装Ubuntu14.5时运行Android Studio可 ...
- TabLayout+ViewPager 标题不显示问题
第一次用TabLayout+ViewPager 组合在布局中写好了三个标题预览没问题而且也设置了 app:tabIndicatorColor="@color/colorAccent" ...
- percona-toolkit(pt-online-schema-change)工具包的安装和使用
1.下载和安装percona toolkit的包 #yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/ ...