<p>Given the <code>root</code> of a binary tree, return <em>the lowest common ancestor of its deepest leaves</em>.</p>

<p>Recall that:</p>

<ul>
	<li>The node of a binary tree is a leaf if and only if it has no children</li>
	<li>The depth of the root of the tree is <code>0</code>. if the depth of a node is <code>d</code>, the depth of each of its children is <code>d + 1</code>.</li>
	<li>The lowest common ancestor of a set <code>S</code> of nodes, is the node <code>A</code> with the largest depth such that every node in <code>S</code> is in the subtree with root <code>A</code>.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png" style="width: 600px; height: 510px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4]
<strong>Output:</strong> [2,7,4]
<strong>Explanation:</strong> 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.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [1]
<strong>Explanation:</strong> The root is the deepest node in the tree, and it&#39;s the lca of itself.
</pre>

<p><strong class="example">Example 3:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree will be in the range <code>[1, 1000]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 1000</code></li>
	<li>The values of the nodes in the tree are <strong>unique</strong>.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as 865: <a href="https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/" target="_blank">https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/</a></p>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    
    void levelorder(TreeNode* root,vector<TreeNode*>& v)
    {
        if(root==NULL)
        {
            return;
        }
        
        
        queue<TreeNode*> q;
        q.push(root);
        q.push(NULL);
        
       vector<TreeNode*> vec;
        
        
        
        while(!q.empty())
        {
            
            TreeNode* temp = q.front();
            q.pop();
            
            if(temp==NULL)
            {
                v=vec;
                
                vec.clear();
                
                if(!q.empty())
                {
                    q.push(NULL);
                }
                
                
                
                
            }else{
                
                
                vec.push_back(temp);
                
                if(temp->left!=NULL)
                {
                    q.push(temp->left);
                }
                
                if(temp->right!=NULL)
                {
                    q.push(temp->right);
                }
                
                
            }
            
            
            
        }
        
    }
    
    
    bool getpath(TreeNode* root,TreeNode* reqroot,vector<TreeNode*>& v)
    {
        
        if(root==NULL)
        {
            return false;
        }
        
        if(root->val==reqroot->val)
        {
            return true;
        }
        
        v.push_back(root);
        
        if(getpath(root->left,reqroot,v)==true)
        {
            return true;
        }
        
        if(getpath(root->right,reqroot,v)==true)
        {
            return true;
        }
        
        v.pop_back();
        
        return false;
        
        
        
    }
    
    
  TreeNode* getlca(vector<TreeNode*> v1,vector<TreeNode*> v2)
    {
        set<TreeNode*> s1;
      
        for(int i=v1.size()-1;i>=0;i--)
        {
            s1.insert(v1[i]);
        }
      
        for(int i=v2.size()-1;i>=0;i--)
        {
            if(s1.count(v2[i]))
            {
                return v2[i];
            }
        }
        
        return NULL;
        
    }
    
    
    
    
    
    
    
    TreeNode* lcaDeepestLeaves(TreeNode* root) {
        
        
        vector<TreeNode*> v;
        
        levelorder(root,v);
        
       if(v.size()==1)
       {
           return v[0];
       }
        
        TreeNode* n1 = v[0];
        TreeNode* n2 = v[v.size()-1];
        
        vector<TreeNode*> v1;
        vector<TreeNode*> v2;
        
        
        getpath(root,n1,v1);
        getpath(root,n2,v2);
        
        
       TreeNode* ans=  getlca(v1,v2);
       
        
        
        return ans;
        
    }
};
22800