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

题目:

Given a binary tree

struct Node {int val;Node *left;Node *right;Node *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.

Example:

Input: {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"$id":"5","left":null,"next":null,"right":{"$id":"6","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}Output: {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":{"$id":"4","left":null,"next":{"$id":"5","left":null,"next":null,"right":null,"val":7},"right":null,"val":5},"right":null,"val":4},"next":{"$id":"6","left":null,"next":null,"right":{"$ref":"5"},"val":3},"right":{"$ref":"4"},"val":2},"next":null,"right":{"$ref":"6"},"val":1}Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B.

Note:

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

题解:

是Populating Next Right Pointers in Each Node的进阶版.

Iteration解法相同.

Time Complexity: O(n). Space: O(1).

AC Java:

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4     public int val;
 5     public Node left;
 6     public Node right;
 7     public Node next;
 8
 9     public Node() {}
10
11     public Node(int _val,Node _left,Node _right,Node _next) {
12         val = _val;
13         left = _left;
14         right = _right;
15         next = _next;
16     }
17 };
18 */
19 class Solution {
20     public Node connect(Node root) {
21         Node cur = root;
22         while(cur != null){
23             Node dummyHead = new Node(); //记录下一层的假头
24             Node it = dummyHead;
25
26             while(cur != null){
27                 if(cur.left != null){
28                     it.next = cur.left;
29                     it = it.next;
30                 }
31
32                 if(cur.right != null){
33                     it.next = cur.right;
34                     it = it.next;
35                 }
36
37                 cur = cur.next;  //cur 更新到下一层假头的next上面
38             }
39
40             cur = dummyHead.next;
41         }
42
43         return root;
44     }
45 }

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4824975.html

LeetCode 117. Populating Next Right Pointers in Each Node II相关推荐

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

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

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

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

  5. [Leetcode][JAVA] Populating Next Right Pointers in Each Node II

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

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

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

  7. LeetCode 116. Populating Next Right Pointers in Each Node

    LeetCode 116. Populating Next Right Pointers in Each Node Solution1:我的答案 迭代版层次遍历 有个2B bug困扰了我好久 clas ...

  8. [Leetcode Week15]Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...

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

最新文章

  1. linux 增量备份镜像,【备份与恢复】合并增量备份与映像副本
  2. NOtePad++快捷键大全
  3. 从CNN视角看在自然语言处理上的应用
  4. Expression.Blend.4 Chapter 图片和视频的使用
  5. IDEA 同一个工程下不同模块之间的类相互调用
  6. YOLO3 动漫人脸识别
  7. 开课吧python小课学了有用吗-和年薪百万的CFO大佬聊天后,我慌了!
  8. android 发送显示广播,如何查看Android系统当前发送了什么广播
  9. 计算机网络基础系列(四)HTTP、七层模型及其内部对应协议
  10. python汉字排序_Python中文排序(转载)
  11. mybatis对象包含list类型属性的resultMap配置
  12. 腾讯是如何一刀刀,在15年间干死那些竞争对手的?! (zz)
  13. html编码器是什么意思,编码器是什么意思
  14. oneos组件系列02:ws2812全彩LED
  15. MySQLzip格式安装包
  16. 如何在Windows 8.1中“忘记”有线(或无线)网络
  17. 【css】fa图标变细
  18. 终端类型 xterm linux,Linux的终端类型
  19. injected stylesheet 导致页面样式异常
  20. Kafka Broker 基本架构二

热门文章

  1. Win7下共享文件(以及凭据管理简单介绍)
  2. 知乎高赞:985计算机视觉毕业后找不到工作怎么办?怒刷leetcode,还是另寻他路?
  3. 深入JVM彻底剖析前面ygc越来越慢的case
  4. 解决springdatajpa 在解析实体类的字段时候驼峰自动转为下划线问题
  5. 对学校的希望和寄语_家长对孩子的期望寄语精选
  6. python不等式编程_在Python中pandas列上的不等式
  7. 使用lucce分词怎么_深度学习时代,分词真的有必要吗
  8. for根据ID去重_Vue中v-for配合key使用的重要性
  9. 试从微型计算机的硬件组成角度谈谈单片机,单片机原理及应用课后习题参考答案1~6章...
  10. 成功解决 ValueError: fill value must be in categories