level order traversal of binary tree

Solutions on MaxInterview for level order traversal of binary tree by the best coders in the world

showing results for - "level order traversal of binary tree"
Maya
07 Sep 2016
1# To import queue datastructure
2import collections
3 
4# Code to implement a binary tree
5class TreeNode: 
6     def __init__(self, val):
7         self.val = val
8         self.left = None
9         self.right = None
10 
11# Function for level Order Traversal
12def levelOrderTraversal(root):
13    ans = []
14 
15    # Return Null if the tree is empty
16    if root is None:
17        return ans
18    
19    # Initialize queue 
20    queue = collections.deque()
21    queue.append(root)
22 
23    # Iterate over the queue until it's empty
24    while queue:
25        # Check the length of queue
26        currSize = len(queue)
27        currList = []
28 
29        while currSize > 0:
30            # Dequeue element
31            currNode = queue.popleft()
32            currList.append(currNode.val)
33            currSize -= 1
34 
35            # Check for left child
36            if currNode.left is not None:
37                queue.append(currNode.left)
38            # Check for right child
39            if currNode.right is not None:
40                queue.append(currNode.right)
41        
42        # Append the currList to answer after each iteration
43        ans.append(currList)
44 
45    # Return answer list
46    return ans
47 
48root = TreeNode(1)
49root.left = TreeNode(2)
50root.right = TreeNode(3)
51root.left.left = TreeNode(4)
52root.left.right = TreeNode(5)
53 
54# Check if the algorithm work
55print(levelOrderTraversal(root))
56
queries leading to this page
when to use level order tree traversallevel order traversal narylevel order traversal of a tree is formed with the help ofnumber of tree traversal in level orderwhat is level order traversal in binary treelevel order traversal of binary tree 27level order traversal algorithmlevel order traversal binary treebinary tree level order traversal 2level order traversal binary 4level oder traversallevel order tree traversalswhat is level order traversal in tree in c 2b 2blevel order traversal level order traversal using inorderbinary tree level order traversal programizimplementing level order traversallevel order traversal bst c 2b 2blevel order traversal from arrayfor the tree below 2c write the level order traversal build binary tree from level order traversalbst level order traversallevel order traversal in treeelevel order traversal treeprint level order traversallevel order traversal is possible with 3flevel order binary tree traversallevel order traversal of a binarylevel order vs in order traversallevel order traversallevel order traversal prcticeimportance of level order traversal binary treewhat is the level order traversalcan we build binary tree by level order and inroder traversalbinary tree level order traversal 28easy 29level order traversal tree in cto perform level order traversal of binary tree which of the following data structure will be requiredprogram for level order traversallevel order traversal of a treelevel order traversal binary search treea level order traversalgiven the level order traversal create a binary treelevel order traversal n ary treelevel order traversal of complete binary treelevel order traversal in c 2b 2bexplain level order traversal types of level order traversalsbinary tree level order traversal iilevelorder traversal of binary treebinary search tree level order traversallevel order traversal in tree line by linelevel traversal of binary treelevel order traversal of tree problemslevel order traversal binary exampleslevel by level traversal of binary treelevel ordere traversal in treelevel ordeer traversallevel order traversal of a binary treelevel order traversal requiregiven level order traversal create binary treelevel order tree traversaltree 2b level order traversalwhen is a level order tree traversal usefulbinary tree level order traversal for each levellevel order traversal binary tree 5clevel order traversal in binary tree level order traversal implementationto perform level order traversal of a binary treehow to do level order traversallevel order traversal for binary search treebinary tree level order traversal solutionlevel by level traversal of binary tree in c 2b 2bwhat is level order traversallevel order traversal in bstcreate binary tree from level order traversallevel order traversal in data structurelevel order traversal in create tree from level order traversallevel order traversal binary tree c 2b 2blevel order traversal codelevel order traversal of binary search tree in c 2b 2blevel order traversal in treelevel order traversal binarylevel order traversal conceptcode for level order traversallevel order traversal in bst c 2b 2bis level order traversal same as inorder traversalhow to do a level order traversal of a binary search treetree level traversallevel order traversal of a binary tree c 2b 2blevel order traversal binary search tree algorithmbinary level order traversallevel order traversal of treelevel order traversal of binary tree level order traversal binary in c 2b 2blevel order traversal write the code of level order traversal of binary treelevel order traversal require which data structurelevel order traversal is also known aslevel order traversal llevel order traversal of bst importanceconstruct binary tree from level order traversallevel by level traversal of binary tree is calledlevel order traversal binary trecode for level order tree traversallevel order traversal level by levellevel order traversal binary tree in clevel of traversal of binary treeprint binary tree with level order traversallevel order traversal using loopto perform level order traversal of a binary tree which data structurebinary tree level order traversalwhat would be the level order traversal of the bstis level order traversal same as inorderbinary tree level order traversal 3tree level order traversalhow to know level in level order traversallevel order traversal bstlevel order traversal of bstlevel order traversal to binary treelevel order traversal of a binary treelevel order traversal of tree in cgiven the level order traversal of a binary tree 28from left to right 29 2clevel order traversal in binary treelevel order and pre order traversal of the same treelevel order traversal c 2b 2btree 3a level order traversallevel order traversal of binary tree