Short Python, beats 100%


  • 0
    class Solution(object):
            def buildTree(self, io, po):
    
                def build(po, io, hm, pstart, istart, size):
                    if not size:
                        return None
                    
                    cur_node = po[pstart + size - 1] 
                    idx, root = hm[cur_node], TreeNode(cur_node)
                    lsize = idx - istart
                    rsize = size - lsize - 1
                    root.left = build(po, io, hm, pstart, istart, lsize)
                    root.right = build(po, io, hm, pstart + lsize, idx + 1, rsize)
                    return root
    
                hm = {v: i for i, v in enumerate(io)}
                return build(po, io, hm, 0, 0, len(po))
    
    # Status: Accepted
    # Runtime: 64ms
    
    

Log in to reply
 

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