跳到主要内容

116. 填充每个节点的下一个右侧节点指针 [medium]

116. 填充每个节点的下一个右侧节点指针 [medium]

https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/

给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:

struct Node {
int val;
Node *left;
Node *right;
Node *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。

初始状态下,所有 next 指针都被设置为 NULL。

示例:

image

输入:{"$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":{"$id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}

输出:{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":{"$id":"4","left":null,"next":{"$id":"5","left":null,"next":{"$id":"6","left":null,"next":null,"right":null,"val":7},"right":null,"val":6},"right":null,"val":5},"right":null,"val":4},"next":{"$id":"7","left":{"$ref":"5"},"next":null,"right":{"$ref":"6"},"val":3},"right":{"$ref":"4"},"val":2},"next":null,"right":{"$ref":"7"},"val":1}

解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。

提示:

  • 你只能使用常量级额外空间。
  • 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。

通过次数39,840 | 提交次数65,188

Third Try

2020-07-17

非递归写法,记录每一层最左侧的位置,然后沿着next扫描该层,实现了类似BFS的效果。

看题解了解到的思路,不错不错。

# 非递归写法
class Solution(object):
def connect(self, root):
"""
:type root: Node
:rtype: Node
"""
if not root:
return root

left_most = root
while left_most.left is not None:
lead_node = left_most
while lead_node:
lead_node.left.next = lead_node.right
if lead_node.next:
lead_node.right.next = lead_node.next.left
lead_node = lead_node.next
left_most = left_most.left
return root
  • 执行用时:52 ms, 在所有 Python 提交中击败了75.99%的用户
  • 内存消耗:15.9 MB, 在所有 Python 提交中击败了50.00%的用户

Second Try

2020-07-17

  • O(1)空间解法,按提示递归不考虑栈的空间占用,看题解需要利用next的属性。
  • 第一波,node的子节点left node和 right node必定相连
  • 第二波,left node的子节点right node和隔壁right node的子节点left node相连。精简起来,就是当前node的right node,和当前node的next node的left node是相连的
  • 利用完美二叉树的定义,不用考虑左右子节点是否有些不存在。如果输入的不是完美二叉树,会有各种bug。
  • 使用dfs递归遍历,一波自然搞定。
"""
# Definition for a Node.
class Node(object):
def __init__(self, val=0, left=None, right=None, next=None):
self.val = val
self.left = left
self.right = right
self.next = next
"""

class Solution(object):
def connect(self, root):
"""
:type root: Node
:rtype: Node
"""
"""
- O(1)空间解法,考虑利用next的属性
- 第一波,left node和 right node必定相连
- 第二波,left node的子节点right node和隔壁next node的子节点left node相连
- 精简起来,就是当前node的right node,和当前node的next node的left node是相连的
- 好像没有第三波了?
- 这个看起来是深度优先遍历?
"""
# 各种利用完美二叉树的定义,不用考虑左右子节点是否有些不存在.
# 如果输入的不是完美二叉树,这个函数就各种报错了
# 如果不存在子节点,直接返回即可
if not (root and root.left):
return root

root.left.next = root.right
if root.next:
root.right.next = root.next.left
self.connect(root.left)
self.connect(root.right)
return root
  • 执行用时:52 ms, 在所有 Python 提交中击败了75.99%的用户
  • 内存消耗:15.9 MB, 在所有 Python 提交中击败了50.00%的用户

First Try

2020-07-17

用另一种不常用的方法进行BFS分层,每次搞完当前队列里的所有元素,刚好就是同一层的元素。当然空间O(N)不满足题目要求。

"""
# Definition for a Node.
class Node(object):
def __init__(self, val=0, left=None, right=None, next=None):
self.val = val
self.left = left
self.right = right
self.next = next
"""

from collections import deque
class Solution(object):
def connect(self, root):
"""
:type root: Node
:rtype: Node
"""
# 如果是O(n)空间,直接bfs搞定;O(1)空间就麻烦了。。
# 还是先用队列来一波吧
if not root:
return root

queue = deque([root])
while len(queue):
for i in range(0, len(queue) - 1):
queue[i].next = queue[i + 1]
for i in range(0, len(queue)):
node = queue.popleft()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return root
  • 执行用时:64 ms, 在所有 Python 提交中击败了25.99%的用户
  • 内存消耗:15.7 MB, 在所有 Python 提交中击败了50.00%的用户
  • improved version

减少一轮循环,而且也是比较正统的队列用法,速度快了一丢丢。

class Solution(object):
def connect(self, root):
"""
:type root: Node
:rtype: Node
"""
# 如果是O(n)空间,直接bfs搞定;O(1)空间就麻烦了。。
# 还是先用队列来一波吧
if not root:
return root

queue = deque([root])
while len(queue):
leadnode = None
for i in range(0, len(queue)):
node = queue.popleft()
if leadnode:
leadnode.next = node
leadnode = node
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return root
  • 执行用时:48 ms, 在所有 Python 提交中击败了88.12%的用户
  • 内存消耗:16 MB, 在所有 Python 提交中击败了50.00%