Problem : Given a binary tree and two node pointers. Return the pointer to their closest ancestor.
method 1: using recursion.
search for the two nodes and keep a count (initialized to zero) if node 1 is found update it by 1 and if node 2 is found update it by 2. when counter's value becomes 3 , we have found our ancestor node, return it.
It took me some while to understand the recursion in this problem but after understanding this, it is pretty obvious.

method 1: using recursion.
search for the two nodes and keep a count (initialized to zero) if node 1 is found update it by 1 and if node 2 is found update it by 2. when counter's value becomes 3 , we have found our ancestor node, return it.
It took me some while to understand the recursion in this problem but after understanding this, it is pretty obvious.
No comments:
Post a Comment