<s> class TreeNode : <EOL> def __init__ ( self , x ) : <EOL> self . val = x <EOL> self . left = None <EOL> self . right = None <EOL> class Solution : <EOL> def buildTree ( self , inorder , postorder ) : <EOL> lookup = { } <EOL> for i , num in enumerate ( inorder ) : <EOL> lookup [ num ] = i <EOL> return self . buildTreeRecu ( lookup , postorder , inorder , len ( postorder ) , <NUM_LIT:0> , len ( inorder ) ) <EOL> def buildTreeRecu ( self , lookup , postorder , inorder , post_end , in_start , in_end ) : <EOL> if in_start == in_end : <EOL> return None <EOL> node = TreeNode ( postorder [ post_end - <NUM_LIT:1> ] ) <EOL> i = lookup [ postorder [ post_end - <NUM_LIT:1> ] ] <EOL> node . left = self . buildTreeRecu ( lookup , postorder , inorder , post_end - <NUM_LIT:1> - ( in_end - i - <NUM_LIT:1> ) , in_start , i ) <EOL> node . right = self . buildTreeRecu ( lookup , postorder , inorder , post_end - <NUM_LIT:1> , i + <NUM_LIT:1> , in_end ) <EOL> return node <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> inorder = [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:3> ] <EOL> postorder = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ] <EOL> result = Solution ( ) . buildTree ( inorder , postorder ) <EOL> print result . val <EOL> print result . left . val <EOL> print result . right . val </s>
