A simple recursive method got WA


  • 0
    L
    # Definition for a  binary tree node
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    mylist = []
    class Solution:
        # @param root, a tree node
        # @return a list of integers
        def postorderTraversal(self, root):
            if not root:
                return []
            self.postorderTraversal(root.left)
            self.postorderTraversal(root.right)
            mylist.append(root.val)
            return my list
    

    When testing for {1,2} got [1,2,1], which supposed to be [2,1]. But this code is correct on my computer.

    Can anyone help me?


  • 0
    S

    If you are going to use a global variable, like mylist = [], remember to clean it. Since LeetCode run all test case in once.


  • 0
    L

    Thank you very much for the answer.


  • 0
    F
    This post is deleted!

Log in to reply
 

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