1

This code intends to perform level order traversal for a tree. I am trying to store both level information and node element in the queue. But code gives error when I try to extract level element from the queue.

Can anyone explain why this code gives error when trying to access second element of tuple ? Is this intended usage of tuples incorrect ?

from collections import defaultdict
from queue import Queue

    class Node:
        def __init__(self,data):
            self.val = data
            self.left = None
            self.right = None

    def levelOrderTraversal(root):
        result = defaultdict(list)
        if not root:
            return result
        q = Queue()
        level = 0
        q.put((level, root))

        while not q.empty():
            e = q.get()
            level = e[0]
            #Above line throws error: TypeError: 'int' object is not subscriptable
            node = e[1]
            result[level].append(node.val)
            if node.left:
                q.put(level + 1, node.left)
            if node.right:
                q.put(level + 1, node.right)
        return result

    def test1():
        root = Node(1)
        root.left = Node(2)
        root.right = Node(3)        

        levelOrderTraversal(root)

    test1()
1
  • What's the error message you get? Commented May 27, 2018 at 15:45

1 Answer 1

1

You are messing up while offering to the queue. Initially you are doing q.put((root,level)) and some other places you are doing q.put(level + 1,node.left),q.put(level + 1,node.right). See the following corrected code:

from collections import defaultdict
from queue import Queue

class Node:
    def __init__(self,data):
        self.val = data
        self.left = None
        self.right = None

def levelOrderTraversal(root):
    result = defaultdict(list)
    if not root:
        return result
    q = Queue()
    level = 0
    q.put((level, root))

    while not q.empty():
        e = q.get()
        level = e[0]
            #Throws error: TypeError: 'Node' object does not support indexing
        node = e[1]
        result[level].append(node.val)
        if node.left:
            q.put((level + 1,node.left))
        if node.right:
            q.put((level + 1,node.right))
    return result

def test1():
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    return levelOrderTraversal(root)

print(test1()) # prints defaultdict(<class 'list'>, {0: [1], 1: [2, 3]})

Working code in Ideone https://ideone.com/Xy0OR8

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for pointing out that error. After the correction, I get a different error: level = e[0] TypeError: 'int' object is not subscriptable
I am not sure whether you are making other mistake. Please see the working code in Ideone i have added
Thanks, there were some other syntax errors. I fixed them. Thanks for looking into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.