Why this problem always errer,wrong answer!Ask for some help!


  • 0
    G
    class Solution:
    # @param root, a tree node
    # @return a list of lists of integers
    def zigzagLevelOrder(self, root):
        if root == None:
            flag = True
        else:
            flag = False
        Node_Level = []
        This_Level = [root]
        Next_Level = []
        while flag == False:
            flag = True
            for i in range(len(This_Level)):
                if This_Level[i].left != None:
                    Next_Level.append(This_Level[i].left)
                    flag = False
                else:
                    pass
                if This_Level[i].right != None:
                    Next_Level.append(This_Level[i].right)
                    flag = False
                else:
                    pass
            Node_Level.append(This_Level)
            This_Level = []
            for j in range(len(Next_Level)):
                This_Level.append(Next_Level[j])
            Next_Level = []
        print '['
        for i in range(len(Node_Level)):
            temp = []
            for j in range(len(Node_Level[i])):
                temp.append(Node_Level[i][j].val)
            if  i % 2 == 1:
                temp.reverse()
            out_str = '  ['
            for k in range(len(temp)-1):
                out_str += str(temp[k])
                out_str += ','
            out_str += str(temp[len(temp)-1])
            out_str += ']'
            
    
            if i != len(Node_Level)-1:
                out_str += ','
            else:
                pass
    
            print out_str
        print ']'

Log in to reply
 

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