[leetcode] 116. Populating Next Right Pointers in Each Node @ python
原题
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相关推荐
- LeetCode 116. Populating Next Right Pointers in Each Node
LeetCode 116. Populating Next Right Pointers in Each Node Solution1:我的答案 迭代版层次遍历 有个2B bug困扰了我好久 clas ...
- 【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 ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- 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 ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...
- 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 ...
- [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 ...
- 116. Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode {TreeLinkNode *left;TreeLinkNode *right;TreeLinkNode *ne ...
- 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 ...
最新文章
- Windows日志及其保护
- 如何查看dll被那个service占用_不小心执行 rm -f,该如何恢复?
- 神经网络 | 过拟合以及 google神经网络小工具
- Parsing error: The keyword 'const' is reservedeslint
- [转载] Python进阶:设计模式之迭代器模式
- esp8266驱动_【直播视频】微信小程序连接阿里云物联网控制esp8266实现rgb调节。...
- innerdb disable error
- karto探秘之open_karto 第四章 --- 回环检测与后端优化
- 电脑怎么分区硬盘分区方法
- 只需3步把VSCode打造成Markdown编辑器
- 关于ubantu安装cmake
- 微信标题特殊符号大全 ✔
- 专家分析 | 半导体芯片短缺不会很快结束
- 微信开发工具BUG(漏洞),魔法加法
- 手游冷知识丨为什么绝大多数手游不开放自由交易系统?
- 2022国赛数学建模思路 - 案例:线性优化-粒子群算法
- 【CISSP】安全运营
- 关于UTF8,UTF16,UTF32,UTF16-LE,UTF16-BE
- js元素offset与client
- AI+遥感:释放每个像元价值