题目

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. C++ 11 Lambda表达式!!!!!!!!!!!

    C++11的一大亮点就是引入了Lambda表达式.利用Lambda表达式,可以方便的定义和创建匿名函数.对于C++这门语言来说来说,“Lambda表达式”或“匿名函数”这些概念听起来好像很深奥,但很多 ...

  2. (转)网站DDOS攻击防护实战老男孩经验心得分享

    网站DDOS攻击防护实战老男孩经验心得分享 原文:http://blog.51cto.com/oldboy/845349

  3. 基于spring-boot和docker-java实现对docker容器的动态管理和监控[附完整源码下载]

    ​ (我是个封面) docker简介 Docker 是一个开源的应用容器引擎,和传统的虚拟机技术相比,Docker 容器性能开销极低,因此也广受开发者喜爱.随着基于docker的开发者越来越多,doc ...

  4. Elasticsearch优化

    2.out of memory错误 因为默认情况下es对字段数据缓存(Field Data Cache)大小是无限制的,查询时会把字段值放到内存,特别是facet查询,对内存要求非常高,它会把结果都放 ...

  5. AJPFX辨析GBK和UTF8的区别

    GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符仅能存储简体中文字符. UTF-8编码:它是一种全国家通过的一种编码,如果你的网站涉及到多 ...

  6. java类及编写public类的基础点

    1.一个java文件中只能有一个public类.且公共类名称必须与java文件名一致,否则会出现错误提示.与其他面向对象编程语言的一样,在利用java分析问题时,基本思路即为将问题的属性(静)与行为( ...

  7. 织梦list/arclist标签调用文章内容

    list标签: 1. 进入后台->模型表单-> 频道模型 -> 内容模型管理 -> 修改对应的模型 2. 列表附加字段-填写body 3. 调用时添加“addfields='b ...

  8. Vmware Player 比较

    .wiz-todo, .wiz-todo-img {width: 16px; height: 16px; cursor: default; padding: 0 10px 0 2px; vertica ...

  9. [opencv bug] orb flannBaseMatcher Exception

    when i use flannBaseMathcer to match 2 sets of orb descriptor, it occured an exception : unsigned lo ...

  10. allure使用简介

    #安装依赖包pip install requests_toolbeltpip install pyyamlpip install pytest-allure-adaptor #安装allure2 说明 ...