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

这一题和上一题Populating next right pointer in each node的区别在于二叉树不是完美二叉树,所以每次的第一个结点不一定是沿着左子树的最左端一直向下。

方法一:

代码原出处没有解释,这里给出个人理解。上一题是这题的特殊情况,所以这题的代码也能通过上一题。

核心思想是:层次遍历。 在上一题中,我们记下当前行的下一行中最左端的结点作为起始点,是为了实现队列的层与层之间的转换功能,这里也同样用这种方式,但因上一题中是满二叉树,所以,我们只需在开头直接取就行,这里我们需判断当前行左右孩子中哪个存在就用哪个。这里值得注意的是,遇到4所在那一层时,cur=4的左右孩子都不存在时,直接取左右孩子中的一个,这时firNext依旧为NULL(因为不知道当前行中的左右孩子是否存在,所以,开始firNext定义为NULL),这时因为最左端的左右孩子都不存在,所以,不需要连接,然后我们在该行中移动cur,直达cur的左右孩子中至少一个存在,我们取存在的那个(若两个都存在,取左孩子)为下一行的最左端结点。

层与层之间的转换解决了,下面我们来解决行之间的移动。如图,若5为根结点的子树为3的右孩子,那如何连接4和5?像上一题中,4(身为2的左孩子),先连接2的右孩子,但其右孩子不存在啊?若是像上题中,那样:左连右,右连next的左,那中间就会有一个NULL !这时,我们定义一个遍历pre,让其完成在行中的穿针引线的作用:pre先指向2的左孩子4,因为2的右孩子不存在,所以,pre不动,直到遇到5.

最外层的while循环是层层之间的转化由firNext负责,层之间的连接由pre负责。

/**
* 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 *cur=root;
while(cur)
{
TreeLinkNode *firNext=nullptr; //下一层的第一个结点
TreeLinkNode *prev=nullptr; //同一层中的前结点
while(cur)
{
if(firNext==nullptr)
firNext=cur->left?cur->left:cur->right;
if(cur->left)
{
if(prev)
prev->next=cur->left;
prev=cur->left;
}
if(cur->right)
{
if(prev)
prev->next=cur->right;
prev=cur->right;
}
cur=cur->next;
}
cur=firNext;
}
}
};

方法二:

递归,虽然不满题意,但有利于熟悉递归算法,依旧写在这。代码来源Grandyang博友。

// Recursion, more than constant space
class Solution {
public:
void connect(TreeLinkNode *root) {
if (!root) return;
TreeLinkNode *p = root->next;
while (p) {
if (p->left) {
p = p->left;
break;
}
if (p->right) {
p = p->right;
break;
}
p = p->next;
}
if (root->right) root->right->next = p;
if (root->left) root->left->next = root->right ? root->right : p;
connect(root->right);
connect(root->left);
}
};

[Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针的更多相关文章

  1. LeetCode: Populating Next Right Pointer in Each Node

    LeetCode: Populating Next Right Pointer in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

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

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

  3. [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  4. LeetCode——Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  5. [leetcode]Populating Next Right Pointers in Each Node II @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...

  6. [LeetCode] [LeetCode] Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  7. LeetCode - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  8. [Leetcode] Populating next right pointer in each node 填充每个节点的右指针

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

  9. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

随机推荐

  1. Apache httpd Server 配置正向代理

    背景 代理(Proxy),位于客户端与实际服务端之间,当客户端需要请求服务端内容时,先向代理发起请求,代理将请求转发到实际的服务器,再原路返回.也可以在代理服务器设置缓存,将实际服务器上不常变化的内容 ...

  2. 【Js】Jquery遍历-each(function(e){})中的e和$(this)的区别

    $("selector").each(function(e){ console.log($(e)); console.log($(this)); console.log(e); c ...

  3. apache使用.htaccess文件中RewriteRule重定向后,URL中的加号无法解析

    今天在使用.htaccess做伪静态的时候,发生一件怪事,URL里存在C++时会有问题,在处理C++这个词的时候,无论如何,$_GET都得不到++,只能得到C空格. 一开始我以为是没用urlencod ...

  4. python文件操作(2017-8-5)

    一.打开文件 open(文件名,模式,编码)#默认模式为只读 f = open("c:/asd.txt") date = f.read() f.close() print(date ...

  5. linux文件操作篇 (二) 打开和关闭文件

    2.1 打开文件和关闭文件 #include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>  头文件 i ...

  6. 使用source命令解决mysql导入乱码问题

    设定编码格式:mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql

  7. python2.7练习小例子(二十一)

        21):1.题目:两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已抽签决定比赛名单.有人向队员打听比赛的名单.a说他不和x比,c说他不和x,z比,请编程序找出三队 ...

  8. 线上环境HBASE-1.2.0出现oldWALs无法自动回收情况;

    正常情况下,hmaster会定期清理oldWALs文件夹,一般该文件大小也就几百兆,但是我们线上 环境出现了该文件没有自动回收情况,如图: 该目录占用hdfs空间多达7.6T,浪费空间: 后来经过多番 ...

  9. find的详细使用

    对我我这个出学者,这个已经算是很难了,不过今天整理了一下,感觉还可以接受. find Linux中十分重要的一个查找功能, [root@moban /]# find /tmp/ -type f -na ...

  10. Mac上配置Cocos2d-x开发环境(多平台:Android/iOS)

    下载以下资源: Cocos2d-x (http://www.cocos2d-x.org) Android NDK(http://developer.android.com/tools/sdk/ndk/ ...