Level Order Traversal is a traversal algorithm in Tree Data Structure, where a tree traversed from left to right for each level one by one.
So level order traversal first traverse the root node which is at level zero and then for level 1 and so on.
There are four way commonly used for traverse the binary tree:
- In Order
- Pre Order
- Post Order
- Level Order
A level is the number of parent nodes corresponding to a given a node of the tree. It is basically the number of ancestors from that node until the root node.
So, root node level is zero, since it has no parents. If it has two children, both children level would be 1, since it has only one ancestor until the root node, which is the root node itself.
Now, you should understand that what is level and how level order traversal traverse the binary tree. Lets take an example:
In the above figure, we start traveling level 0 i.e. root node.C++ implementation:
No comments:
Post a Comment