Given the root of a 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. 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.


&nbsp;
Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation: We return the node with value 2, colored in yellow in the diagram.
The nodes coloured in blue are the deepest leaf-nodes of the tree.
Note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but the depth of nodes 7 and 4 is 3.

Example 2:

Input: root = [1]
Output: [1]
Explanation: The root is the deepest node in the tree, and it's the lca of itself.


Example 3:

Input: root = [0,1,3,null,2]
Output: [2]
Explanation: The deepest leaf node in the tree is 2, the lca of one node is itself.


&nbsp;
Constraints:


	The number of nodes in the tree will be in the range [1, 1000].
	0 &lt;= Node.val &lt;= 1000
	The values of the nodes in the tree are unique.


&nbsp;
Note: This question is the same as 865: https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/

class Solution:
    def lcaDeepestLeaves(self, root: Optional[TreeNode]) -> Optional[TreeNode]:

        self.max_lvl = (0,[])
        self.pathes = {}
        def rec(root,parent,lvl):
            if not root:
                return
            if lvl > self.max_lvl[0]:
                self.max_lvl = (lvl,[root])
            elif lvl == self.max_lvl[0]:
                self.max_lvl = (lvl,self.max_lvl[1]+[root])
            self.pathes[root] = parent
            rec(root.left,root,lvl+1)
            rec(root.right,root,lvl+1)
        rec(root,None,0)
        print(self.max_lvl)
        # for key in self.pathes:
        # if key!=None and self.pathes[key]!=None:
        # print(key.val,"-",self.pathes[key].val)
        if len(self.max_lvl[1]) < 2:
            return self.max_lvl[1][0]
        parent = self.max_lvl[1]
        while len(parent) > 1:
            temp = set()
            for p in parent:
                temp.add(self.pathes.get(p,None))
            parent = temp
        return parent.pop()
class Solution {
    public TreeNode lcaDeepestLeaves(TreeNode root) {
        if (root.left == null && root.right == null) return root;
        int depth = findDepth(root);
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int count = 0;
        while (!q.isEmpty()) {
            int size = q.size();
            count++;
            if (count == depth) {
                break;
            }
            for (int i = 0; i < size; i++) {
                TreeNode cur = q.poll();
                if (cur.left != null) q.offer(cur.left);
                if (cur.right != null) q.offer(cur.right);
            }
        }
        Set<Integer> set = new HashSet<>();
        while (!q.isEmpty()) {
            set.add(q.poll().val);
        }
        return find(root, set);
    }

    public int findDepth(TreeNode root) {
        if (root == null) return 0;
        int left = findDepth(root.left);
        int right = findDepth(root.right);
        return 1 + Math.max(left, right);
    }

    public TreeNode find(TreeNode root, Set<Integer> set) {
        if (root == null) return root;
        if (set.contains(root.val)) return root;
        TreeNode left = find(root.left, set);
        TreeNode right = find(root.right, set);
        if (left != null && right != null) return root;
        else if (left != null) return left;
        else if (right != null) return right;
        else return null;
    }
}
class Solution {
public:
    
    stack<TreeNode *>st;
    
    vector<TreeNode * >vec;
    int mx = INT_MIN;
    
    void ch(TreeNode * root, int h){
        if(!root) return ;
        ch(root->left, h+1);
        ch(root->right, h+1);
        
        if(h==mx){
            vec.push_back(root);
        }
        else if(h>mx){
            vec.clear();
            vec.push_back(root);
            mx = h;
        }
    }
    
    bool uf = 0;
    
    void check(TreeNode * root, TreeNode * it){
        
        if(!root or uf) return ;
        
        st.push(root);
        
        if(root->val == it->val){
            uf = 1;
            return ;
        }
        
        check(root->left, it);
        check(root->right, it);
        
        if(!uf)
            st.pop();
    }
    
    TreeNode* lcaDeepestLeaves(TreeNode* root) {
        vec.clear();
        ch(root, 0);
        vector<vector<TreeNode *>>ans;
        ans.clear();
        int mnx = INT_MAX;
        for(auto it:vec){
            uf = 0;
            check(root, it);
            vector<TreeNode *>temp;
            
            while(!st.empty()){
                temp.push_back(st.top());
                st.pop();
            }
            reverse(temp.begin(), temp.end());
            ans.push_back(temp);
            int p = temp.size();
            mnx = min(mnx, p);
        }
        
        TreeNode * rt = new TreeNode(0);
        if(ans.size() == 1){
            return ans[0][ans[0].size()-1];
        }
        bool lb = 0;
        
        
        for(int j=0; j<mnx; j++){
            for(int i=0; i<ans.size()-1; i++){
                
                if(ans[i][j]==ans[i+1][j]){
                    continue;
                }
                else{
                    rt = ans[i][j-1];
                    lb = 1;
                    break;
                }
            }
            if(lb) break;
        }
        
        return rt;
        
    }
};
var lcaDeepestLeaves = function(root) {
    if(!root) return root;
    // keep track of max depth if node have both deepest node
    let md = 0, ans = null;
    const compute = (r = root, d = 0) => {
        if(!r) {
            md = Math.max(md, d);
            return d;
        }

        const ld = compute(r.left, d + 1);
        const rd = compute(r.right, d + 1);

        if(ld == rd && ld == md) {
            ans = r;
        }

        return Math.max(ld, rd);
    }
    compute();
    return ans;
};
Lowest Common Ancestor of Deepest Leaves