Python Solution (Accepted - not sure if complete)

  • 0
    # 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:
            index_of_max = nums.index(sorted(nums, reverse=True)[0])
            left_arr = nums[:index_of_max]
            right_arr = nums[index_of_max + 1:]
            node = TreeNode(nums[index_of_max])
            node.left = self.constructMaximumBinaryTree(left_arr)
            node.right = self.constructMaximumBinaryTree(right_arr)
            return node```
    This was accepted as a solution, however when browsing through other solutions, it feels to me as if this is lacking/incomplete/might fail some time
    I have only recently started practicing binary trees algorithms and would like to know how to improve. What do I need to change in my mindset in order to see the recursive solutions clearer.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.