原题

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.
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
Example:

Given the following perfect binary tree,

 1

/
2 3
/ \ /
4 5 6 7
After calling your function, the tree should look like:

 1 -> NULL

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

解法1

BFS, 按层遍历, 将每层的节点加到q, 然后对q遍历, 将每个节点指向下一个节点, 最后一个节点指向None.
Time: O(n)
Space: O(1)

代码

# Definition for binary tree with next pointer.
# class TreeLinkNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#         self.next = Noneclass Solution:# @param root, a tree link node# @return nothingdef connect(self, root):if not root:returnq = [root]while q:for i in range(1, len(q)):q[i-1].next = q[i]q[-1].next = Nonenew_q = []for node in q:if node.left:new_q.append(node.left)if node.right:new_q.append(node.right)q = new_q

解法2

BFS, 简化版

代码

"""
# Definition for a Node.
class Node(object):def __init__(self, val, left, right, next):self.val = valself.left = leftself.right = rightself.next = next
"""
class Solution(object):def connect(self, root):""":type root: Node:rtype: Node"""# base caseif not root: return Noneq = [root]while q:for i in range(1, len(q)):q[i-1].next = q[i]q = [kid for node in q for kid in (node.left, node.right) if kid]return root

解法3

DFS

代码

"""
# Definition for a Node.
class Node(object):def __init__(self, val, left, right, next):self.val = valself.left = leftself.right = rightself.next = next
"""
class Solution(object):def connect(self, root):""":type root: Node:rtype: Node"""# base caseif not root: returnif root.right:root.left.next = root.rightif root.next:root.right.next = root.next.leftself.connect(root.left)self.connect(root.right)return root

[leetcode] 116. Populating Next Right Pointers in Each Node @ python相关推荐

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

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

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

  3. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

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

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

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

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

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

  8. 116. Populating Next Right Pointers in Each Node

    题目: Given a binary tree struct TreeLinkNode {TreeLinkNode *left;TreeLinkNode *right;TreeLinkNode *ne ...

  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. Windows日志及其保护
  2. 如何查看dll被那个service占用_不小心执行 rm -f,该如何恢复?
  3. 神经网络 | 过拟合以及 google神经网络小工具
  4. Parsing error: The keyword 'const' is reservedeslint
  5. [转载] Python进阶:设计模式之迭代器模式
  6. esp8266驱动_【直播视频】微信小程序连接阿里云物联网控制esp8266实现rgb调节。...
  7. innerdb disable error
  8. karto探秘之open_karto 第四章 --- 回环检测与后端优化
  9. 电脑怎么分区硬盘分区方法
  10. 只需3步把VSCode打造成Markdown编辑器
  11. 关于ubantu安装cmake
  12. 微信标题特殊符号大全 ✔
  13. 专家分析 | 半导体芯片短缺不会很快结束
  14. 微信开发工具BUG(漏洞),魔法加法
  15. 手游冷知识丨为什么绝大多数手游不开放自由交易系统?
  16. 2022国赛数学建模思路 - 案例:线性优化-粒子群算法
  17. 【CISSP】安全运营
  18. 关于UTF8,UTF16,UTF32,UTF16-LE,UTF16-BE
  19. js元素offset与client
  20. AI+遥感:释放每个像元价值

热门文章

  1. Lecture 07 08 RNN---Hinton课程
  2. java 输出大于n的质数_Java 计算并打印第n个质数
  3. 关于HTTP重定向至HTTPS
  4. 04刘笑维-05刘洪雨-实训一
  5. 带孔的打印纸怎么设置_oki打印机打印带孔纸如何缩小纸张间距
  6. 案例06:大球自转+小球公转+移动
  7. 用Python实现URL Encoding和Decoding
  8. 敏捷方法 - 敏捷的理念
  9. 100%基于深度强化学习的对冲基金
  10. 浅入Elasticsearch(全文检索服务器)