Why my python solution index out of range in line 27


  • 0
    F
    # Definition for binary tree with next pointer.
    # class TreeLinkNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    #         self.next = None
    
    class Solution(object):
        def connect(self, root):
            """
            :type root: TreeLinkNode
            :rtype: nothing
            """
            que=[]
            numSize=0
            if root!=None:
                que.append(root)
                while que:
                    quelen=len(que)
                    numSize=0
                    for numSize in range(quelen):
                        tmp=que.pop(0)
                        if numSize==(quelen-1):
                            tmp.next=None
                        else:
                            tmp.next=que[numSize+1]
                        if tmp.left:
                            que.append(tmp.left)
                        if tmp.right:
                            que.append(tmp.right)

Log in to reply
 

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