Spread the post

Find ancestor of longest path of binary tree

Given a binary tree, and get the longest path of two leaf node ancestor. Suppose following data are inserted of binary tree [90, 10, 80, 77, 60, 30, 50].

Example :


Output [80].

9010 80Longest path ancestor77506030


C program to get longest path ancestor.


Code execution: view code execution process.

Stack Areamainpath_node(pointer)result (int) =5root(pointer)long_ancestora (int) =1b (int) =3path_node(pointer)result(pointer)temp(pointer) Heap Areastruct Treedata (int)= 90left_child (pointer) right_child (pointer) struct Treedata (int)= 10left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 80left_child (pointer) right_child (pointer) struct Treedata (int)= 77left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 60left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 30left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 50left_child (pointer)= NULLright_child (pointer)= NULL

Note that not given all step of execution process here. view more.

Try it yourself

Spread the post

Recommended Posts: