题目:

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,

         1
/ \
2 3
/ \ \
4 5 7

After calling your function, the tree should look like:

         1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL

代码:

/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
if (!root) return;
deque<TreeLinkNode *> curr, next;
curr.push_back(root);
while ( !curr.empty() )
{
TreeLinkNode dummy(-);
TreeLinkNode *pre = &dummy;
while ( !curr.empty() )
{
TreeLinkNode *tmp = curr.front(); curr.pop_front();
pre->next = tmp;
if (tmp->left) next.push_back(tmp->left);
if (tmp->right) next.push_back(tmp->right);
pre = tmp;
}
pre->next = NULL;
std::swap(curr, next);
}
}
};

tips:

广搜思路(有些违规,因为不是const extra space)

每一层设立一个虚拟头结点,出队的同时pre->next = tmp

=====================================

学习了另外一种思路,可以不用队列的数据结构,这样就符合const extra space的条件了。代码如下:

/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
TreeLinkNode *pre;
TreeLinkNode *curr;
TreeLinkNode *next_first; // store next level's first not null node
curr = root;
while ( curr ){
// move to next tree level
TreeLinkNode dummy(-);
pre = &dummy;
next_first = NULL;
// connect the curr level
// record the first not null left or right child of the curr level as for the first node of next level
while ( curr ){
if ( !next_first ){
next_first = curr->left ? curr->left : curr->right;
}
if ( curr->left ){
pre->next = curr->left;
pre = pre->next;
}
if ( curr->right ){
pre->next = curr->right;
pre = pre->next;
}
curr = curr->next;
}
curr = next_first;
}
}
};

tips:

这套代码的大体思路是数学归纳法

1. root节点的root->next按照题意是NULL

2. 处理root的left节点和right节点之间的next关系

...如果知道了第n-1层的node之间的next关系,则可以得到第n层node节点之间的next关系...

按照这套思路,就可以写出上述的代码。

这里有两个细节需要注意:

a. next_first不一定是left还是right,这个要注意,只要next_first一直为NULL就要一直找下去。

b. 设立一个dummy虚node节点,令pre指向dummy,可以不用判断pre为NULL的情况,简化判断条件。

另,回想为什么level order traversal的时候必须用到队列,可能就是因为没有每一层之间的next关系。

==========================================

第二次过这道题,直接看常数空间复杂度的思路,重新写了一遍AC。

/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
TreeLinkNode* curr = root;
while ( curr )
{
TreeLinkNode* pre = new TreeLinkNode();
TreeLinkNode* next_level_head = NULL;
while ( curr )
{
if ( !next_level_head )
{
next_level_head = curr->left ? curr->left : curr->right;
}
if ( curr->left )
{
pre->next = curr->left;
pre = pre->next;
}
if ( curr->right )
{
pre->next = curr->right;
pre = pre->next;
}
curr = curr->next;
}
curr = next_level_head;
}
}
};

【Populating Next Right Pointers in Each Node II】cpp的更多相关文章

  1. 【遍历二叉树】12往二叉树中添加层次链表的信息【Populating Next Right Pointers in Each Node II】

    本质上是二叉树的层次遍历,遍历层次的过程当中把next指针加上去. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

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

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

  3. 【leetcode】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

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

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

  6. 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...

  7. Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...

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

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

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

随机推荐

  1. Linux下tftp安装与配置

    1. 背景 开发板在u-boot下从pc获取文件的方式有三种: 1)dnw传输:http://www.cnblogs.com/tanghuimin0713/p/3614768.html 2)串口传输: ...

  2. Hive[2] 基础介绍

    2.3 Hive 内部介绍: P44 $HIVE_HOME/lib 下的 jar 文件是具体的功能部分:(CLI模块) 其它组件,Thrift 服务,可以远程访问其他进程功能:也有使用 JDBC 和 ...

  3. Objective-C代码的文件扩展名

  4. 开篇 hello 内Cool超人

    经过一年时间看到asp.net mvc一直被受微软开发团队的注重.与之相比的silverlight我感觉到有点力不从心.除去silverlight第一次运行要安装Runtime不说,产品不可能只运行在 ...

  5. luigi学习2-在hadoop上运行Top Artists

    一.AggregateArtistsHadoop class AggregateArtistsHadoop(luigi.contrib.hadoop.JobTask): date_interval = ...

  6. C++排列对称串

    题目内容:字符串有些是对称的,有些是不对称的,请将那些对称的字符串按从小到大的顺序输出.字符串先以长度论大小,如果长度相同,再以ASCII码值为排序标准. 输入描述:输入数据中含有一些字符串(1< ...

  7. MySQL远程访问授权

    开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要确定防火墙没有阻止 3306 端口,否则远程是无法通过 330 ...

  8. Java命名:

    如果没有public类,就和遵循文件名命名规则: 1.一个.java文件只能有1个public class(暗示可以没有) 2.如果有public class,那么文件名必须与修饰符为public的类 ...

  9. pandas聚合和分组运算——GroupBy技术(1)

    数据聚合与分组运算——GroupBy技术(1),有需要的朋友可以参考下. pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对数据集进行切片.切块.摘要等操作.根据一个或多个 ...

  10. 简单翻译和补充:1. GNU ARM Eclipse

    原文链接: GNU ARM Eclipse GNU 介绍: GNU 计划,又称革奴计划,是由RichardStallman在1983年9月27日公开发起的.它的目标是创建一套完全自由的操作系统.Ric ...