Same Code as a LCA Question Just find deepest two Nodes and get their LCA & done || Runs 0ms
Approach
It is a same as a LCA question but here we will have to find the nodes who are deepest in levels So, their LCA will be the root who is at shortest dist
![image.png](https://assets.leetcode.com/users/images/598eb32a-3f36-4a08-970e-6edc1529c19e_1743987390.3951874.png)

# Approach
<!-- Describe your approach to solving the problem. -->
- It is a same as a LCA question but here we will have to find the nodes who are deepest in levels So, their LCA will be the root who is at shortest distance from them.
- So, we can simply find the two nodes which are in deepest levels.
- Then we can find those node's LCA & thats it done 

# Complexity
- Time complexity:  **O(N)**
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: **O(H)**
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
    int maxDepth = 0;
    TreeNode* deepestLeave1 = NULL;
    TreeNode* deepestLeave2 = NULL;
public:
    void GetDeepestLeaves(TreeNode* &root, int depth){
        if(!root){
            return;
        }

        if(!root->left && !root->right){
// If we found the node depth greater than maxDepth then pick that node
            if(depth > maxDepth){
                maxDepth = depth;
                deepestLeave1 = deepestLeave2 = root;
            }
// if we found that depth again we can simply update our deepestLeaves2 since deepestLeaves1 is already picked another node.
            else if(depth == maxDepth){
                deepestLeave2 = root;
            }
            return;
        }

        GetDeepestLeaves(root->left, depth+1);
        GetDeepestLeaves(root->right, depth+1);
    }
    
// This is a normal/common LCA code to find LCA of two nodes 
    TreeNode* Solve(TreeNode* &root){
        if(!root || root == deepestLeave1 || root == deepestLeave2){
            return root;
        }

        TreeNode* left = Solve(root->left);
        TreeNode* right = Solve(root->right);

        if(left && right){
            return root;
        }
        else if(left){
            return left;
        }
        return right;
    }
    
    TreeNode* subtreeWithAllDeepest(TreeNode* root) {
        GetDeepestLeaves(root, 0);
        return Solve(root);
    }
};
```
![image.png](https://assets.leetcode.com/users/images/8f45f11e-c625-4599-9b28-e830bc2c66d0_1724569032.8579876.png)
['Tree', 'Depth-First Search', 'Binary Tree', 'C++']