100% faster C++ depth comparison, optimized with hashmap
\nunordered_map<int,int> mp;\n    \n    int depth(TreeNode* root){\n        if(!root) return 0;\n        if(mp.find(root->val)!=mp.end()) return mp[root->val];\
```\nunordered_map<int,int> mp;\n    \n    int depth(TreeNode* root){\n        if(!root) return 0;\n        if(mp.find(root->val)!=mp.end()) return mp[root->val];\n        return mp[root->val] = 1 + max(depth(root->left), depth(root->right));\n    }\n    TreeNode* subtreeWithAllDeepest(TreeNode* root) {\n        if(!root) return root;\n        \n        int l,r;\n        \n        if(root->left && mp.find(root->left->val)!=mp.end()) l = mp[root->left->val];\n        else l = depth(root->left);\n        \n        if(root->right && mp.find(root->right->val)!=mp.end()) r = mp[root->right->val];\n        else r = depth(root->right);\n  \n        if(l==r)return root;\n        else if(l>r) return subtreeWithAllDeepest(root->left);\n        else return subtreeWithAllDeepest(root->right);\n    }\n```
[]