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

转载于:https://www.cnblogs.com/love-yh/p/6985403.html

[Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针相关推荐

  1. Leetcode: Populating Next Right Pointers in Each Node II

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

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

  3. LeetCode Populating Next Right Pointers in Each Node II(dfs)

    问题:给出一个二叉查找树,将结点与其右边的结点相连 思路: 从顶向上,从右向左的方式 .递归过程中,在当前结点及父结点作为参数传递.在向下的过程中,如果父结点不为空则获取当前结点的next结点.如果当 ...

  4. 【To Understand!】LeetCode 117. Populating Next Right Pointers in Each Node II

    LeetCode 117. Populating Next Right Pointers in Each Node II Solution1:我的答案 层次遍历 /*** Definition for ...

  5. leetcode 117. 填充每个节点的下一个右侧节点指针 II(Populating Next Right Pointers in Each Node II)...

    目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树 struct Node { int val; Node left; Node right; Node *next; } 填充它的每个 ne ...

  6. 117 Populating Next Right Pointers in Each Node II

    117 Populating Next Right Pointers in Each Node II 就是 Bibary Tree Level order Traverse class Solutio ...

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

    原题链接在这里:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题目: Given a bi ...

  8. LeetCode OJ - Populating Next Right Pointers in Each Node II

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

  9. LeetCode Populating Next Right Pointers in Each Node(dfs)

    问题:给出一个满二叉查找树,将结点与其右边的结点相连 思路: 一种方式是自顶向下,基于当前结点和右边结点来作遍历,对于根结点,右边结点为null,不管是否有右边结点,在递归过程中都需要将当前结点的ne ...

最新文章

  1. 手工卸载.Net写的win服务
  2. java 高手_Java高手是怎样炼成的
  3. NSMapTable
  4. QGraphicsView加入到布局,所在的窗口变大,视图和场景都变大,使场景中的矩形也变大
  5. ElasticSearch(二十四)基于scoll技术滚动搜索大量数据
  6. 666! 玩王者,识英雄,这样也能上顶会!
  7. sharepoint webpart
  8. 蓝牙:深入浅出低功耗蓝牙(BLE)协议栈
  9. 芯烨打印机api密钥php,CCXT中文开发手册
  10. 3种常用的Redis缓存读写策略
  11. 倍福PLC控制台达EtherCAT伺服案例分析
  12. jQuery图片LightBox插件 点击图片放大 支持移动手机
  13. 好用的pdf阅读器(便携)
  14. python tts 离线 linux_ubuntu16.04安装科大讯飞Linux SDK实现离线语音合成(TTS)
  15. python数据采集2-HTML解析
  16. C语言实现复数计算器
  17. html命名锚记链接失败,命名锚记(设置命名锚记超级链接)
  18. ESD门禁管理系统方案
  19. php获得视频分辨率,php+ffmpeg 获取视频相关信息(缩略图、视频分辨率)
  20. java jtextarea字体_Jtextarea如何设置不同字体、颜色

热门文章

  1. Android图片的Base64编码与解码
  2. 判断点是否在三角形内
  3. Vue.js仿QQ音乐(移动端)
  4. nmon--linux压力测试工具
  5. EMA算法的C#实现
  6. jboss4中手动部署EJB(jboss4.0.2+ejb2.0+j2sdk5.0+xpsp2)
  7. oracle批处理还原数据库,用批处理写的:数据库备份还原工具(修正加强版)
  8. html网页如何传递接收地址参数
  9. linux 容器_Linux容器的幕后花絮
  10. 菌群多样性检测_多样性丰富了中学Linux用户群