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 ...
随机推荐
- springboot springmvc 抛出全局异常解决方法
springboot中抛出异常,springboot自带的是springmvc框架,这个就不多说了. springmvc统一异常解决方法这里要说明的是.只是结合了springboot的使用而已.直接上 ...
- 一道笔试题和UML思想 ~
一句软件工程界的名言,让我想起了一个和一道笔试题有关的故事.希望更多的人了解 UML 背后的思想比他的语法更重要,是笔者写作本文的一点小愿望. 一.从一句软件工程名言说起 对很多事情的处理上,东西方都 ...
- 解决mysql本地数据库不能用ip访问的问题
[转]http://gone18611.blog.163.com/blog/static/1851943120104705244116/ MYSQL数据库缺省安装后,其默认用户名ROOT如果只能以&l ...
- w3c万维网的介绍和html基本构成
怎么与浏览器交互? 1.鼠标 2.键盘输入 w3c标准: 中文名:万维网联盟!外文名:world wide web cansortium万维网联盟创建于1994年,是web技术领域最具权威个影响的国际 ...
- springboot集成shiro实现权限认证
github:https://github.com/peterowang/shiro 基于上一篇:springboot集成shiro实现身份认证 1.加入UserController package ...
- 关于rabbitmq的消息路由的同步问题
http://www.cnblogs.com/me-sa/archive/2012/11/12/rabbitmq_ram_or_disk_node.html我是看了上面的博客明白了一些原理的,我之前一 ...
- ES6 Interator
Interator "集合"数据的结构主要有 Array .Object. Set and Map ,任何数据结构只要部署 Iterator 接口,就可完成遍历操作 遍历过程: 创 ...
- codevs 4888 零件分组
4888 零件分组 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 现有一些棍状零件,每个零件都有 ...
- countUp 动画展示数字变化
html <p id="countUp" style="font-size:25px;height:25px;background-color:#0aa;" ...
- 手机安全卫士——在设置中心 自定义view和自定义属性
自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载 ...