美文网首页
03_填充每个节点的下一个右侧节点指针

03_填充每个节点的下一个右侧节点指针

作者: butters001 | 来源:发表于2019-11-12 10:52 被阅读0次
"""
# Definition for a Node.
class Node(object):
    def __init__(self, val, left, right, next):
        self.val = val
        self.left = left
        self.right = right
        self.next = next
"""


class Solution(object):
    def connect(self, root):
        """
        :type root: Node
        :rtype: Node
        """
        def helper(root):
            if not root:
                return
            if not root.left:
                return
            root.left.next = root.right
            if root.next:
                root.right.next = root.next.left
            helper(root.left)
            helper(root.right)
        helper(root)
        return root

相关文章

网友评论

      本文标题:03_填充每个节点的下一个右侧节点指针

      本文链接:https://www.haomeiwen.com/subject/uuufictx.html