题目

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);
}
};

GitHub测试程序源码

LeetCode(117) Populating Next Right Pointers in Each Node II的更多相关文章

  1. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  2. LeetCode(117):填充同一层的兄弟节点 II

    Medium! 题目描述: 给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *n ...

  3. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  4. 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 ...

  5. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  6. 【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 ...

  7. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

  8. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

  9. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

随机推荐

  1. Jenkins执行yarn install报错:error An unexpected error occurred:"... ... :Unexpected end of JSON input"

    解决方式: # cd /usr/local/n/versions/node/11.6.0/lib/(node的安装目录下) # rm -rf node_modules # yarn cache cle ...

  2. 博弈论 && 题目

    终于我也开始学博弈了,说了几个月,现在才学.学多点套路,不深学.(~~) 参考刘汝佳蓝书p132 nim游戏: 假设是两维的取石子游戏,每次可以在任意一堆拿任意数量个(至少一根,因为这样游戏的状态集有 ...

  3. (转)nginx应用总结(2)--突破高并发的性能优化

    原文:http://www.cnblogs.com/kevingrace/p/6094007.html 在日常的运维工作中,经常会用到nginx服务,也时常会碰到nginx因高并发导致的性能瓶颈问题. ...

  4. [转]Todd.log - a place to keep my thoughts on programming 分布式架构中的幂等性

    Todd.log - a place to keep my thoughts on programming 理解HTTP幂等性 基于HTTP协议的Web API是时下最为流行的一种分布式服务提供方式. ...

  5. elasticsearch dump加过滤条件(--searchBody)出错的解决 Unexpected token ' in JSON at position 0

    环境:本文测试在es2.4,win10下进行 es dump导数据可以加过滤条件,只导满足条件的数据.方法是用—searchBody参数,值是查询时的查询条件的json格式,例如 然而按官网和网上的格 ...

  6. 如何快速构建CMBD系统-glpi

    一.CMBD系统构建步骤 起初,开发这套CMBD系统是为了帮助朋友公司简化设备统计操作,以代替人工入库方式.举个例子,单位发放笔记本,或者设备更换了硬盘,都需要人工签到,手动输入统计,安装了CMBD系 ...

  7. SPEC CPU 2006编译perl 出错:undefined reference to `pow'

    问题来源: 编译spec2006时,出现如下错误: cc -L/home/yrtan/benchmark/2006/CPU2006v1.0.1/tools/output/lib -L/usr/loca ...

  8. Java 反射机制(一)

    阅读<Core Java Volume I --- Fundamentals>反射部分,总觉得许多概念艰涩难懂.模棱两可.我想造成这个结果的主要原因可能是Cay S. Horstmann和 ...

  9. SharePoint 2010 缺少站点保存为模板选项

    如果您尝试在SharePoint Server 2010中保存网站,并且没有看到"将网站另存为模板"选项,则可能是因为该网站已启用发布功能.如果使用站点发布功能,则有几个选项可将网 ...

  10. App测试流程及测试点

    1 APP测试基本流程 1.1流程图 接收版本 尽快申请到正式环境下测试 不符 App测试版本送测规范 用户行为统计测试 后台订单统计测试 尽快申请到正式环境下测试 兼容性测试.性能压力测试 功能测试 ...