Leetcode-114.二叉树展开为链表(Python)

题目链接

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def flatten(self, root: Optional[TreeNode]) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        if not root:
            return None
        self.flatten(root.left)
        self.flatten(root.right)
        temp=root.right
        root.right=root.left
        root.left=None
        while root.right:
            root=root.right
        root.right=temp

你可能感兴趣的:(leetcode,leetcode,链表,算法,python)