Spread the post

Lowest common ancestor (LCA) of a binary tree

Lowest common ancestor of a binary tree. using queue.

For example Given binary tree are contain following nodes.

13624578

View pointers and nodes of tree.

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

Try it Yourself

C program for print left view of binary tree using level order traversal. Using queue.


Output

view process

Stack Areamainhead(pointer)root(pointer)tail(pointer)lca_ancestorauxiliary1(pointer)auxiliary2(pointer)find_status (int) =1head(pointer)node1 (int) =4node2 (int) =8temp(pointer) NULL Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer) struct Treedata (int)= 3left (pointer)= NULLright (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer) right (pointer)= NULLstruct Treedata (int)= 6left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 7left (pointer)= NULLright (pointer) struct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Queuestatus (int)= 0link (pointer) parent (pointer)= NULLnext (pointer) struct Queuestatus (int)= 1link (pointer) parent (pointer) next (pointer) struct Queuestatus (int)= 0link (pointer) parent (pointer) next (pointer) struct Queuestatus (int)= 1link (pointer) parent (pointer) next (pointer) struct Queuestatus (int)= 0link (pointer) parent (pointer) next (pointer) struct Queuestatus (int)= 0link (pointer) parent (pointer) next (pointer) struct Queuestatus (int)= 1link (pointer) parent (pointer) next (pointer) struct Queuestatus (int)= 1link (pointer) parent (pointer) next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: