Lowest Common Ancestor
IDeserve
0:00 / 0:00
Lowest Common Ancestor
86 105 просмотров · 11 лет назад
IDeserve
38,6 тыс. подписчиков
86 105 просмотров · 11 лет назад
Problem:
Given a binary tree and 2 tree nodes A and B(assuming both nodes A and B are present in the tree), find the lowest common ancestor of the nodes.
Solution:
Traverse the tree in bottom up approach. If node ( A or B ) is found, return it to its parent. Parent will check if it was able to get nodes from both of its child.If yes, then Parent is LCA. If no, Parent will return NULL if none of its child returned A or B ELSE will return not NULL node.
Code: http://www.ideserve.co.in/learn/lowes...
Website: http://www.ideserve.co.in
Facebook: / ideserve.co.in