Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Find nth node of inorder traversal without recursion

Given a binary tree, Our goal is to find the inorder nth node from this given binary tree. For example given Binary tree are contain following nodes.

12435768

Inorder : [4, 2, 1, 5, 7, 6, 8].

Result

Function: Iterative function are to find nth In-order nodes.

View pointers and nodes of Linked list.

Stack Areamainroot(pointer)top(pointer) NULL Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer)= NULLstruct Treedata (int)= 3left (pointer) right (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer) struct Treedata (int)= 6left (pointer)= NULLright (pointer) struct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULL

Try it Yourself

Program for find nth node of inorder traversal without using recursion. Using a stack.


Output

View process

Stack Areamainroot(pointer)top(pointer)find_nth_nodesnth (int) =5status (int) =5temp(pointer)top(pointer) Heap Areastruct Treedata (int)= 1left (pointer) right (pointer) struct Treedata (int)= 2left (pointer) right (pointer)= NULLstruct Treedata (int)= 3left (pointer) right (pointer) struct Treedata (int)= 4left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 5left (pointer)= NULLright (pointer) struct Treedata (int)= 6left (pointer)= NULLright (pointer) struct Treedata (int)= 7left (pointer)= NULLright (pointer)= NULLstruct Treedata (int)= 8left (pointer)= NULLright (pointer)= NULLstruct Stacklink (pointer) next (pointer)= NULLstruct Stacklink (pointer) next (pointer)

Try it Yourself

Spread the post

Recommended Posts: