Given a binary tree

struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • Recursive approach is fine, implicit stack space does not count as extra space for this problem.

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

题意:

给定一个任意二叉树,其中每个节点都有next指针。

设法将next指针指向同一层右侧相邻节点。

思路:

递归,解法很巧妙。

以 1 为curr,用pre来连接curr.left:2 和 curr.right:3。因为此时根节点 1的next指向null, 退出for循环
            1  ( curr )--->null
          / \
dummy(-1):pre--> 2---> 3
         / \ \
         4 5 7 递归调用connect(dummy.next),因为dummy.next指向2,此时 2 为curr。用pre来连接curr.left:4 和 curr.right:5。
                   1
           / \
2(curr)---> 3
         / \ \
dummy(-1):pre--> 4--->5 7
继续走for循环, curr = curr.next, 此时curr为3。 继续用pre连接 curr.left(3的左子树为Null) 和 curr.right:7。
                   1  
             /    \
2 ---> 3(curr)
         / \ \
dummy(-1):pre--> 4--->5 --->7
因为此时curr的next指向null, 退出for循环。
继续递归调用connect(dummy.next)... 直至 root == null, return

代码:

 public class Solution {
public void connect(TreeLinkNode root) {
if (root == null) return; TreeLinkNode dummy = new TreeLinkNode(-1);
for (TreeLinkNode curr = root, prev = dummy;
curr != null; curr = curr.next) {
if (curr.left != null){
prev.next = curr.left;
prev = prev.next;
}
if (curr.right != null){
prev.next = curr.right;
prev = prev.next;
}
}
connect(dummy.next);
}
}



[leetcode]117. Populating Next Right Pointers in Each NodeII用next填充同层相邻节点的更多相关文章

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

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

  2. Java for LeetCode 117 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 117 Populating Next Right Pointers in Each Node II ----- java

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

  4. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

  5. 117. Populating Next Right Pointers in Each Node II 计算右边的附属节点

    [抄题]: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNod ...

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

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

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

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

  9. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

随机推荐

  1. Ubuntu14.04下hadoop-2.6.0单机配置和伪分布式配置

    需要重新编译的教程:http://blog.csdn.net/ggz631047367/article/details/42460589 在Ubuntu下创建hadoop用户组和用户 hadoop的管 ...

  2. 第15章 高并发服务器编程(1)_非阻塞I/O模型

    1. 高性能I/O (1)通常,recv函数没有数据可用时会阻塞等待.同样,当socket发送缓冲区没有足够多空间来发送消息时,函数send会阻塞. (2)当socket在非阻塞模式下,这些函数不会阻 ...

  3. 半联结&反联结!

    半联结是在两个数据集(表)之间的联结,其中第一个数据集中的数据行在决定是否返回时会根据在另一个数据集中出现或不出现至少一个相匹配的数据行来确定.“不出先”匹配行——这是半联结的一种特殊形式,称为反联结 ...

  4. 你的GAN训练得如何--GAN 的召回率(多样性)和精确率(图像质量)方法评估

    生成对抗网络(GAN)是当今最流行的图像生成方法之一,但评估和比较 GAN 产生的图像却极具挑战性.之前许多针对 GAN 合成图像的研究都只用了主观视觉评估,一些定量标准直到最近才开始出现.本文认为现 ...

  5. ORM介绍(字段 和 字段的参数)

    ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述 ...

  6. 代码:cookie(一个广告展示例子)

    这个小例子的要求是: 用户第一次进入,显示大图,2秒后大图动画关闭,再把小图动画展开: 用户再次进入后,只显示静态小图. 做法: 定义一个容器 .img201512ad,宽高写死:1190x70.—— ...

  7. OpenACC 计算圆周率(简单版)

    ▶ 书上的计算圆周率的简单程序,主要是使用了自定义函数 #include <stdio.h> #include <stdlib.h> #include <math.h&g ...

  8. 使用JsonViewer来格式化json字符串

    1. 在线转换 https://www.bejson.com/jsonviewernew/ ==>格式化 2. 使用notepad ++ 的jsonViewer插件 下载地址 http://ww ...

  9. VB6 内存释放

    VB在内存释放方面是这样的, 首先和其他任何语言一样,如果是在stack分配的空间的话,操作系统自动进行管理 比如下面的语句 dim a as string '当a 超出它的作用范围以后,就会被释放掉 ...

  10. binlog、redo log、undo log区别

    root@(none) 04:17:18>show variables like 'innodb_log_group_home_dir';+--------------------------- ...