LintCode: 将二叉树拆成链表

LintCode: 将二叉树拆成链表

Python

""" Definition of TreeNode: class TreeNode: def __init__(self, val): this.val = val this.left, this.right = None, None """
class Solution:
    # @param root: a TreeNode, the root of the binary tree
    # @return: nothing
    def flatten(self, root):
        # write your code here
        L = []
        self.pre_order(root, L)
        p1 = root
        for i in range(1, len(L)):
            p2 = TreeNode(L[i])
            p1.left = None
            p1.right = p2
            p1 = p1.right
        return root


    def pre_order(self, root, L):
        if root == None:
            return
        L.append(root.val)
        self.pre_order(root.left, L)
        self.pre_order(root.right, L)

你可能感兴趣的:(二叉树)