Python刷leetcode--剑指 Offer 07.重建二叉树

树的问题需要多往递归上想。
然后中序遍历是三种遍历中最关键的遍历。
中序遍历找到先序遍历后 先序遍历之后的几个 = 中序遍历左边的几个

Python刷leetcode--剑指 Offer 07.重建二叉树_第1张图片

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def buildTree(self, preorder, inorder):
        # def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
        # leetcode submit region end(Prohibit modification and deletion)
        if not preorder:
            return None
        node = TreeNode(preorder[0])
        index = inorder.index(preorder[0])
        l_p = preorder[1:index + 1]
        r_p = preorder[index + 1:]
        l_i = inorder[0:index]
        r_i = inorder[index + 1:]
        node.left = self.buildTree(l_p, l_i)
        node.right = self.buildTree(r_p, r_i)
        return node


def range_tree(root):
    if not root:
        return
    range_tree(root.left)
    print(root.val)
    range_tree(root.right)


if __name__ == '__main__':
    sol = Solution()
    preorder = [3, 9, 20, 15, 7]
    inorder = [9, 3, 15, 20, 7]
    ans = sol.buildTree(preorder, inorder)
    range_tree(ans)
    #  前序遍历 preorder = [3,9,20,15,7]
    #  中序遍历 inorder = [9,3,15,20,7]

你可能感兴趣的:(Python,算法与数据结构)