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,
After calling your function, the tree should look like:
分析
为满二叉树添加线索;
由题目可知,线索是根据层序链接,每一层终止节点线索为空,其余节点的next为其层序的下一个节点;
利用层序遍历解决该问题;类似于LeetCode(102) Binary Tree Level Order Traversal。
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:
void connect(TreeLinkNode *root) {
if (!root)
return;
//定义两个队列,一个存储父节点
queue<TreeLinkNode *> parent;
parent.push(root);
root->next = NULL;
while (!parent.empty())
{
//定义队列存储当前子层
queue<TreeLinkNode*> curLevel;
while (!parent.empty())
{
TreeLinkNode *tmp = parent.front();
parent.pop();
if (tmp->left)
curLevel.push(tmp->left);
if (tmp->right)
curLevel.push(tmp->right);
}//while
parent = curLevel;
while (!curLevel.empty())
{
TreeLinkNode *p = curLevel.front();
curLevel.pop();
if (!curLevel.empty())
p->next = curLevel.front();
else
p->next = NULL;
}//while
}//while
}
};
LeetCode(116) Populating Next Right Pointers in Each Node的更多相关文章
- 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 ...
- 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 ...
- [LeetCode] 116&117. Populating Next Right Pointers in Each Node I&II_Medium tag: BFS(Dont know why leetcode tag it as DFS...)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LeetCode(116):填充同一层的兄弟节点
Medium! 题目描述: 给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *n ...
- 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 笔记 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 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 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 ...
随机推荐
- scrapy框架的命令行解释
scrapy框架的命令解释 创建爬虫项目 scrapy startproject 项目名例子如下: scrapy startproject test1 这个时候爬虫的目录结构就已经创建完成了,目录结构 ...
- CD4051的切换时间
CD4051:1 2 4 5 12 13 14 15 8个选择IO输入/输出端:3:I/O6:片选低电平有效,搞定平所有通道不通9 10 11:地址选择:功能:通过地址选择译码8个中的某个通道与3脚 ...
- HDU 5230 ZCC loves hacking 大数字的整数划分
http://acm.hdu.edu.cn/showproblem.php?pid=5230 把题目简化后,就是求 1---n - 1这些数字中,将其进行整数划分,其中整数划分中不能有重复的数字,如果 ...
- auth_basic 认证
shell > yum -y install httpd-tools # 安装 htpasswd 工具 shell > cd /usr/local/nginx-/conf shell &g ...
- Java .class文件的反编译与反汇编
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10840818.html 一:反编译 通常用于第三方JAR包的逆向工程. 一般我们拿到的jar包都是经过编译后 ...
- dubbo源码阅读之集群(故障处理策略)
dubbo集群概述 dubbo集群功能的切入点在ReferenceConfig.createProxy方法以及Protocol.refer方法中. 在ReferenceConfig.createPro ...
- var声明提前 undefined
1.同一代码块内,所有var声明都提前: 2.var 变量的初始化不提前,按顺序执行: 3."undefined"和undefined都存在于window中: 4.if(" ...
- Vuex.js状态管理共享数据 - day8
VScode文件目录: amount.vue代码如下: <template> <div> <!-- <h3>{{ $store.state.count }}& ...
- CF1080D Olya and magical square
思路: 构造. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; ll sum[]; int ...
- azure 创建redhat镜像帮助
为 Azure 准备基于 Red Hat 的虚拟机 从 Hyper-V 管理器准备基于 Red Hat 的虚拟机 先决条件 本部分假定你已经从 Red Hat 网站获取 ISO 文件并将 RHEL 映 ...