Issue with Level Order Traversal Implementation menu

User Tag List

Results 1 to 1 of 1
  1. #1
    sheen8's Avatar Banned
    Reputation
    1
    Join Date
    Mar 2023
    Posts
    8
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue with Level Order Traversal Implementation

    Hello everyone,

    I've been working on implementing level order traversal in my code, following the article. However, I'm facing some difficulties and would appreciate some assistance.

    Here's my code implementation so far
    Code:
    from collections import deque
    
    class Node:
        def __init__(self, val):
            self.val = val
            self.left = None
            self.right = None
    
    def levelOrderTraversal(root):
        if root is None:
            return []
    
        result = []
        queue = deque()
        queue.append(root)
    
        while queue:
            level_size = len(queue)
            current_level = []
    
            for _ in range(level_size):
                current_node = queue.popleft()
                current_level.append(current_node.val)
    
                if current_node.left:
                    queue.append(current_node.left)
                if current_node.right:
                    queue.append(current_node.right)
    
            result.append(current_level)
    
        return result
    
    # Example usage
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    
    print(levelOrderTraversal(root))
    The problem I'm facing is that when I run this code, I'm not getting the expected output. It seems like the level order traversal is not working correctly. I've reviewed the code multiple times and compared it to the algorithm described in the blog post, but I couldn't identify the issue.

    Could someone please take a look at the code and point out any potential mistakes or suggest improvements? I'm open to any suggestions or alternative implementations that could help me resolve this problem.

    Issue with Level Order Traversal Implementation

Similar Threads

  1. [Question] Issue with implementing the KMP algorithm
    By YreiauW in forum WoW EMU Questions & Requests
    Replies: 2
    Last Post: 06-22-2023, 04:03 PM
  2. [Selling] 🔥 PROMO — FREE Mythic+10 with ANY order! — Mythic+🔥 Raids🔥 Leveling 🔥Gearing🔥!!!
    By arcaneboost in forum World of Warcraft Buy Sell Trade
    Replies: 0
    Last Post: 05-16-2019, 02:33 PM
  3. [How To] Issue with level up screen shots.
    By duffmanohyeah89 in forum TurboHUD Support
    Replies: 5
    Last Post: 06-22-2018, 10:51 AM
  4. Free level 90 NOW with pre-order
    By Kazique in forum World of Warcraft General
    Replies: 6
    Last Post: 03-11-2014, 07:37 AM
  5. [Buying] Order & Chaos I WILL BUY ACCOUNT WITH LEVEL 60
    By majcherek in forum General MMO Buy Sell Trade
    Replies: 0
    Last Post: 12-15-2011, 04:26 AM
All times are GMT -5. The time now is 03:32 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search