2019年2月28日 654. Maximum Binary Tree

比较简单的树拆分生成,我发现递归的思路我是比较有感觉的。。


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

import operator

class Solution(object):
    
    def buildTree(self, nums):
        if len(nums) == 0:
            return None
        index, value = max(enumerate(nums), key=operator.itemgetter(1))
        
        root = TreeNode(value)
        root.left = self.buildTree(nums[:index])
        root.right = self.buildTree(nums[index+1:])
    
        return root

    
    def constructMaximumBinaryTree(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        return self.buildTree(nums)
上一篇:性能瓶颈分析方法


下一篇:基于AM335X,如何搭建优良的Linux开发环境(下)