题目

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. NET Core迁移

      向ASP.NET Core迁移   有人说.NET在国内的氛围越来越不行了,看博客园文章的浏览量也起不来.是不是要转Java呢? 没有必要扯起语言的纷争,Java也好C#都只是语言是工具,各有各的 ...

  2. 在linux上怎么查看tomcat日志

    进入到tomcat的logs文件夹 tail -f catalina.out

  3. 详解Java构造方法为什么不能覆盖,我的钻牛角尖病又犯了....

    一 看Think in Java,遇到个程序 class Egg2 { protected class Yolk { public Yolk() { System.out.println(" ...

  4. SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题(一)

    当前后端分离时,权限问题的处理也和我们传统的处理方式有一点差异. 笔者前几天刚好在负责一个项目的权限管理模块,现在权限管理模块已经做完了,我想通过5-6篇文章,来介绍一下项目中遇到的问题以及我的解决方 ...

  5. SpringMVC简介01

    SpringMVC也叫Spring Web mvc,属于表现层的框架.SpringMVC是Spring框架的一部分,是在Spring3.0后发布的. Spring结构图: SpringMVC架构: S ...

  6. VS2012,更新补丁后的残忍--创建项目未找到与约束匹配的导出

    解决方法网址:http://blog.csdn.net/jly4758/article/details/18660945

  7. 就来推荐一本2018年研究的Web书《移动Web前端高效开发实战》

    一线互联网公司Web前端团队实战经验总结,涵盖移动Web前端开发各个关键技术环节,包括移动开发核心技术.常用布局方案.MV*类新时代框架.预编译技术.性能优化.开发调试.混合式应用.单元测试.工程化等

  8. Android tess_two Android图片文字识别

    文字识别一般都用的tesseract-ocr. GitHub:https://github.com/tesseract-ocr/tesseract 而Android对应的比较推荐的有个tess-two ...

  9. 使用as开发jni入门(附验证):配置ndk开发环境,配置as相关jni配置

    编写jni,生成so文件: 1.通过as内置的Android SDK下载需要使用的ndk,在系统环境变量设置相关参数 2.新建一个普通as项目,新建一个类,用来静态加载so库和书写本地native方法 ...

  10. centos 离线安装 mysql 5.7

    1 . 安装新版mysql前,需将系统自带的mariadb-lib卸载. rpm -qa|grep mariadb mariadb-libs--.el7.centos.x86_64 rpm -e -- ...