https://github.com/emahtab/lowest-common-ancestor-of-deepest-leaves
Find the lowest common ancestor of deepest leaves
https://github.com/emahtab/lowest-common-ancestor-of-deepest-leaves
bfs breadth-first-search leetcode lowest-common-ancestor problem-solving
Last synced: 4 months ago
JSON representation
Find the lowest common ancestor of deepest leaves
- Host: GitHub
- URL: https://github.com/emahtab/lowest-common-ancestor-of-deepest-leaves
- Owner: eMahtab
- Created: 2020-06-19T09:40:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-19T10:46:33.000Z (over 5 years ago)
- Last Synced: 2025-06-05T05:18:56.883Z (6 months ago)
- Topics: bfs, breadth-first-search, leetcode, lowest-common-ancestor, problem-solving
- Homepage:
- Size: 57.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Lowest common ancestor of deepest leaves
## https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves
Given a rooted binary tree, return the lowest common ancestor of its deepest leaves.
Recall that:
The node of a binary tree is a leaf if and only if it has no children
The depth of the root of the tree is 0, and if the depth of a node is d, the depth of each of its children is d+1.
The lowest common ancestor of a set S of nodes is the node A with the largest depth such that every node in S is in the subtree with root A.
```
Example 1:
Input: root = [1,2,3]
Output: [1,2,3]
Explanation:
The deepest leaves are the nodes with values 2 and 3.
The lowest common ancestor of these leaves is the node with value 1.
The answer returned is a TreeNode object (not an array) with serialization "[1,2,3]".
Example 2:
Input: root = [1,2,3,4]
Output: [4]
Example 3:
Input: root = [1,2,3,4,5]
Output: [2,4,5]
```
**Constraints:**
1. The given tree will have between 1 and 1000 nodes.
2. Each node of the tree will have a distinct value between 1 and 1000.
### Examples : Lowest Common Ancestor node of deepest leaves is highlighted in orange color.
#### e.g. 1

#### e.g. 2

#### e.g. 3

#### e.g. 4

#### e.g. 5

## Approach : Two pass through binary tree
1. **First Pass :** In first pass we move from root to deepest leaves by Level order traversal using BFS. After first pass, `deepestLevelNodes` queue will have leaf nodes at the deepest level
2. **Second Pass :** In second pass we move upwards in the tree, from nodes at the deepest level to root node. To move upwards, we add the parent of the node in `deepestLevelNodes` queue. We keep doing this untill there is only one TreeNode in `deepestLevelNodes` queue.
# Implementation : Iterative
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode lcaDeepestLeaves(TreeNode root) {
if(root == null)
return root;
Map parentMap = new HashMap<>();
parentMap.put(root, null);
Queue q = new ArrayDeque<>();
q.add(root);
Queue deepestLevelNodes = null;
// First Pass : Level order traversal using BFS
// After first pass, deepestLevelNodes queue will have leaf nodes at the deepest level
while(!q.isEmpty()) {
deepestLevelNodes = new ArrayDeque<>(q);
int size = q.size();
for(int i = 0; i < size; i++) {
TreeNode node = q.poll();
if(node.left != null) {
parentMap.put(node.left, node);
q.add(node.left);
}
if(node.right != null) {
parentMap.put(node.right, node);
q.add(node.right);
}
}
}
// Second Pass : We move upwards in the tree, from nodes at the deepest level to root node.
// To move upwards, we add the parent of the node in deepestLevelNodes queue.
// We keep doing this untill there is only one TreeNode in deepestLevelNodes queue.
while(deepestLevelNodes.size() > 1) {
Set set = new HashSet<>();
int size = deepestLevelNodes.size();
for(int i = 0; i < size; i++) {
set.add(parentMap.get(deepestLevelNodes.poll()));
}
for(TreeNode node : set)
deepestLevelNodes.add(node);
}
return deepestLevelNodes.poll();
}
}
```
# References :
https://www.youtube.com/watch?v=QF7ZBH8mXHE