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 ...
随机推荐
- [软件工程基础]2017.10.30 第三次 Scrum 会议
决议 游心与李煦通沟通生成报告脚本问题,并调试相应代码 李煦通部署服务器,并做一定安全检查 石奇川设计实验流程和题库前端页面 王嘉睿爵测试网站基本流程,提出关于用户体验方面的建议 刘子渊阅读代码,为机 ...
- POJ-3275:Ranking the Cows(Floyd、bitset)
Ranking the Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3301 Accepted: 1511 ...
- python之函数名,闭包、迭代器
一.函数名的运用(第一类对象) 函数名是一个变量,但它是一个特殊的变量,与括号配合可以执行函数的变量. 1,函数名的内存地址: def func(): print("呵呵") pr ...
- nodejs express session用法(含保存到redis)
普通用法: var express = require('express'); var session = require('express-session'); var app = express( ...
- go实现生产者消费者
package main import ( "fmt" "math/rand" ) func main() { ch := make(chan int) don ...
- Ajax 提交表单【包括文件上传】
利用js插件实现 <script src="@Url.Content("~/js/layer/jquery.form.min.js")"></ ...
- SourceInsight主题设置
自己经常忘记怎样设置SourceInsight主题,这次一定要记住! 0. 退出SourceInsight软件1. 替换配置文件操作:拷贝Global.CF3到“我的文档\Source Insight ...
- 设置umask
umask 002 例子:umask为003,建立的文件与目录权限是什么? umask为003,所有去掉的属性为-------wx,因此 文件 -rw-rw-r-- 目录 drwxrwxr--
- COGS 788. 昵称
788. 昵称 ★☆ 输入文件:nickname.in 输出文件:nickname.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] ZSUQ送信者与腾讯QQ相似 ...
- 解决activeandroid no such table
场景:activeandroid拷贝数据库 (1)复制sql数据库到项目的assets目录,例如/myapp/src/main/assets/prepop.db (2)确保manifest的AA_DB ...