主要是类还是不熟
看清楚__init__的输入参数是什么
其次是输出,原来不用输出树枝的叉叉之类。。而且class TreeNode也不用取消注释。。
# Definition for a binary tree node.
#class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def constructMaximumBinaryTree(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums:
return None
index = nums.index(max(nums))
root = TreeNode(nums[index])
root.left = self.constructMaximumBinaryTree(nums[:index])
root.right = self.constructMaximumBinaryTree(nums[index+1:])
return root
314ms 11.13%
但是并查不到更优解。。