Lowest Common Ancestor
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/lowest-common-ancestor-binary-tree
Website: http://www.ideserve.co.in
Facebook: https://www.facebook.com/IDeserve.co.in
Видео Lowest Common Ancestor канала IDeserve
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/lowest-common-ancestor-binary-tree
Website: http://www.ideserve.co.in
Facebook: https://www.facebook.com/IDeserve.co.in
Видео Lowest Common Ancestor канала IDeserve
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
![Programming Interview Question: How to print all diagonal's sums for a given binary tree?](https://i.ytimg.com/vi/YwyxxZiAwm0/default.jpg)
![Level Order Traversal](https://i.ytimg.com/vi/46CyhfdgBkQ/default.jpg)
![Reverse a Linked List - Iterative](https://i.ytimg.com/vi/uJZMxWhYTJk/default.jpg)
![Programming Interview Question: Searching a 2D Sorted Matrix](https://i.ytimg.com/vi/ZhG1M_FzxgI/default.jpg)
![Implement a fair coin given an unfair coin](https://i.ytimg.com/vi/zhGBw_9pdpQ/default.jpg)
![Buying and selling stocks](https://i.ytimg.com/vi/76-CYD0jn7s/default.jpg)
![Binary Search](https://i.ytimg.com/vi/vh0jtsTXhno/default.jpg)
![Minimum length subarray of an unsorted array sorting which results in complete sorted array](https://i.ytimg.com/vi/Hg03KTli9co/default.jpg)
![Find an element in a sorted rotated array without finding pivot (minimum element)](https://i.ytimg.com/vi/MIFm82gUM8o/default.jpg)
![Maximum size square sub-matrix with all 1s](https://i.ytimg.com/vi/aYnEO53H4lw/default.jpg)
![Building Bridges Dynamic Programming](https://i.ytimg.com/vi/w6tSmS86C4w/default.jpg)
![Leaders in an array](https://i.ytimg.com/vi/lIe2Dv4_6-o/default.jpg)
![Find intersection of two Linked Lists - O(A + B) Time Complexity and O(1) Space Complexity](https://i.ytimg.com/vi/l936ym8yDSo/default.jpg)
![Create a balanced Binary Search Tree (BST) from a sorted array](https://i.ytimg.com/vi/VCTP81Ij-EM/default.jpg)
![Next greater element in an array](https://i.ytimg.com/vi/8P-Z7Oc8x9I/default.jpg)
![Spiral level order traversal of a binary tree](https://i.ytimg.com/vi/lxIzRD8hO6I/default.jpg)
![Programming Interview Question: Recover Binary Search Tree](https://i.ytimg.com/vi/LR3K5XAWV5k/default.jpg)
![Detect a loop in a linked list](https://i.ytimg.com/vi/apIw0Opq5nk/default.jpg)
![Programming Interview Question: Find intersection of two Linked Lists](https://i.ytimg.com/vi/50HKqI_LOQs/default.jpg)
![Find an element in a sorted rotated array](https://i.ytimg.com/vi/6pSzsJH56BA/default.jpg)