c++ solution || easy to understand
class Solution {\npublic:\n    TreeNode subtreeWithAllDeepest(TreeNode root) {\n        unordered_map mp;\n        mp[root]=NULL;\n        queue q;\n        q.p
class Solution {\npublic:\n    TreeNode* subtreeWithAllDeepest(TreeNode* root) {\n        unordered_map<TreeNode*,TreeNode*> mp;\n        mp[root]=NULL;\n        queue<TreeNode*> q;\n        q.push(root);\n        while(!q.empty()){\n            int n=q.size();\n            for(int i=0;i<n;i++){\n                TreeNode* r=q.front(); q.pop();\n                if(r->left){\n                    q.push(r->left);\n                    mp[r->left]=r;\n                } \n                if(r->right){\n                   q.push(r->right);\n                    mp[r->right]=r;\n                } \n            }\n        }\n        queue<TreeNode*> q1;\n        q1.push(root);\n      vector<TreeNode*> v1;\n        while(!q1.empty()){\n            v1.clear();\n            int n=q1.size();\n            for(int i=0;i<n;i++){\n                TreeNode* r=q1.front(); q1.pop();\n                v1.push_back(r);\n                if(r->left) q1.push(r->left);\n                if(r->right) q1.push(r->right);\n            }\n        }\n      int n=v1.size()-1;\n      TreeNode* t=NULL;\n      unordered_map<TreeNode*,int> mp1;\n      while(t==NULL){\n        for(int i=0;i<v1.size();i++){\n          if(mp1[v1[i]]==n && v1[i]!=NULL) t=v1[i];\n          mp1[v1[i]]++;\n          v1[i]=mp[v1[i]];\n        }\n      }\n      return t;\n    }\n};
[]