Spread the post

Check if all leaf nodes are at same level

Find if all the leaf nodes are at same level in binary tree. without using recursion using queue.

For example given Binary tree are contain following nodes.

13762485910

Result : Leaf nodes are not at same level

Another example

1367245

result: Leaf nodes are at same level

This function are check all leaves are at same level or not.

View leaf nodes.

1367245

View pointers and nodes of Binary tree.

Stack Areamainhead(pointer) NULLroot(pointer)tail(pointer) NULL Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer) right (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULL

Try it Yourself

program for check if leaf nodes are at same level in binary tree. without recursion.


Output

View process

Stack Areamainhead(pointer)root(pointer)tail(pointer)leaf_levellevel (int) =2temp(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer) right (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULLstruct Queuelevel (int)= 0link (pointer) next (pointer) struct Queuelevel (int)= 1link (pointer) next (pointer) struct Queuelevel (int)= 1link (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) next (pointer) struct Queuelevel (int)= 2link (pointer) next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: