Line 48: AttributeError: 'list' object has no attribute 'val'


  • 0
    C
    # 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 buildTree(self, preorder, inorder):
            """
            :type preorder: List[int]
            :type inorder: List[int]
            :rtype: TreeNode
            """
            if len(preorder)==0:
                return []
            root=TreeNode(preorder[0])
    
            if len(preorder)==1:
            	return root
    
            rootInIdx=inorder.index(preorder[0])
            # preorder[lastNodeInLeftIdx] is the largest number in left subtree
            lastNodeInLeftIdx=preorder.index(inorder[rootInIdx-1])
    
    
            root.left=self.buildTree(preorder[1:lastNodeInLeftIdx+1],inorder[:rootInIdx])
            root.right=self.buildTree(preorder[lastNodeInLeftIdx+1:],inorder[rootInIdx+1:])
    
    
            return root
    
    a=Solution()
    print a.buildTree([1,2],[2,1]).val
    print a.buildTree([1,2],[2,1]).left.val
    print a.buildTree([1,2],[2,1]).right
    

    whats wrong with this solution?


Log in to reply
 

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