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 tree could be any binary tree? Would your previous solution still work?
Note:
You may only use constant extra space.
For example,
Given the following binary tree
分析
为一颗给定的二叉树的每个节点添加next节点,next指针指向该节点所在二叉树层的下一个节点。
注意,题目要求空间复杂度为常量。
用两种方法解决该问题:
方法一,借助queue数据结构存储每一层的节点,遍历该层节点逐个添加next指针。该方法空间复杂度是O(n)的,因为每个节点都需要在队列中保存一次。
方法二:不利用额外的空间存储节点,直接操作二叉树,参考链接算法出自网址
AC代码
class Solution {
public:
//方法一:利用层次遍历的思想
void connect1(TreeLinkNode *root) {
if (!root)
return;
else if (!root->left && !root->right)
{
root->next = NULL;
return;
}
queue<TreeLinkNode *> qt;
qt.push(root);
while (!qt.empty())
{
queue<TreeLinkNode *> tmp;
TreeLinkNode *p = qt.front();
//把 当前节点的 左右子节点压入临时队列
if (p->left)
tmp.push(p->left);
if (p->right)
tmp.push(p->right);
qt.pop();
while (!qt.empty())
{
TreeLinkNode *q = qt.front();
p->next = q;
p = q;
//把 当前节点的 左右子节点压入临时队列
if (q->left)
tmp.push(q->left);
if (q->right)
tmp.push(q->right);
qt.pop();
}
p->next = NULL;
qt = tmp;
}//while
return;
}
//方法二:直接操作二叉树节点
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (root == NULL)
return;
TreeLinkNode *p = root;
TreeLinkNode *q = NULL;
TreeLinkNode *nextNode = NULL;
while (p)
{
if (p->left)
{
if (q)
q->next = p->left;
q = p->left;
if (nextNode == NULL)
nextNode = q;
}
if (p->right)
{
if (q)
q->next = p->right;
q = p->right;
if (nextNode == NULL)
nextNode = q;
}
p = p->next;
}
connect(nextNode);
}
};
LeetCode(117) Populating Next Right Pointers in Each Node II的更多相关文章
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(117):填充同一层的兄弟节点 II
Medium! 题目描述: 给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *n ...
- 【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 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】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: 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 II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
随机推荐
- PartTime_网址_内
http://www.360doc.com/content/15/0930/12/28012971_502432950.shtml 2015所有适合程序员接私活的网站 请把 @ 换成 . 猪八戒 ...
- 转 sqlplus/RMAN/lsnrctl 等工具连接缓慢
AIX上sqlplus /as sysdba rman target / 或者lsnrctl start时或者通过sqlplus system/oracle@orcl这样通过监听连接等方式来登陆 ...
- jQuery:如何给动态生成的元素绑定事件?
jQuery的html()可以给现在元素附加新的元素,innerHTML也可以,那么,如何给这些新生成的元素绑定事件呢?直接在元素还未生成前就绑定肯定是无效的,因为所绑定的元素目前根本不存在. 然而, ...
- 开源分布式Job系统,调度与业务分离-HttpJob.Agent组件介绍以及如何使用
项目介绍: Hangfire:是一个开源的job调度系统,支持分布式JOB!! Hangfire.HttpJob 是我针对Hangfire开发的一个组件,该组件和Hangfire本身是独立的.可以独立 ...
- left join \ right join \ inner join 详解
left join 和 left outer join 的区别 通俗的讲: A left join B 的连接的记录数与A表的记录数同 A right join B ...
- MariaDB 实现主从复制
實驗目的: MariaDB為MySQL的一個分支,其完全開源.無版權之虞且操作上與 MySQL 一脈相承,實際應用中非常廣泛,軟件本身很小,安裝容易,使用簡單. 但其也有缺點,指令行方式操作,無原生G ...
- Error: EPERM: operation not permitted,
转载自:https://blog.csdn.net/dong923700243/article/details/78989332 npm ERR! path E:\React\ReactNativeP ...
- JAVA分包下项目部分代码存储
一.注册时姓名去重和符合汉字格式: // 新用户申请加入 public void NewHuman() { System.out.println("========新会员申请加入页面==== ...
- [20190625]记录npm的一些常用命令
1. 安装依赖包 npm install -g packageName //全局安装npm install packageName --save //安装在项目下并写入package.json文件中n ...
- Oracle创建用户、表(1)
Oracle创建用户.表(1) 1. 连接 C:\Users\LEI>sqlplus / as sysdba SQL*Plus: Release 12.1.0.2.0 Production on ...