刷题 | leetcode 114 二叉树展开为链表

给你二叉树的根结点 root ,请你将它展开为一个单链表:

展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。
展开后的单链表应该与二叉树 先序遍历 顺序相同。
 

示例 1:

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

刷题 | leetcode 114 二叉树展开为链表_第1张图片

输入:root = [1,2,5,3,4,null,6]
输出:[1,null,2,null,3,null,4,null,5,null,6]

解题思路:树的前序遍历

# 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: TreeNode) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        if not root:
            return root
        nodelist = []

        def dfs(node):
            if not node:
                return
            nodelist.append(node)
            dfs(node.left)
            dfs(node.right)

        dfs(root)

        result = root 
        tem = result
        for i in range(1, len(nodelist)):
            pre,curr = nodelist[i-1],nodelist[i]
            pre.left = None
            pre.right = curr
            pre = curr


你可能感兴趣的:(leetcode,leetcode,python)