BEATS 80% -- RECURSIVE SOLUTION -- INTUTIVE -- EASY
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n   see comments for exp
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n <B>  see comments for explanation!</B>\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int height(TreeNode* root) {  // find height of tree\n        if (root == nullptr) return 0;\n        else return 1 + max(height(root->left), height(root->right));\n    }\n\n    TreeNode* dfs(TreeNode* root, int h, int curh) {\n        if (root == nullptr) { // if reached to end node having height less than max height!\n            return nullptr;\n        } else {\n            curh++;  // increase current height by 1\n            TreeNode* lef = dfs(root->left, h, curh); //check whether left subtree has that max depth node or not\n        \n            TreeNode* rig = dfs(root->right, h,curh); // check whether right subtree has max depth node or not\n\n            if (curh == h) {\n                return root; // if present node is max depth return it!\n            } else if (lef != nullptr && rig != nullptr) {\n                return root; // if both left nad right subtree have that "max depth node" return their ancestor!\n            } else if (lef != nullptr) {\n                return lef;// if left subtree has "max depth node" return the pointer which was generated recursively\n            } else if (rig != nullptr) {\n                return rig; // apropos to above step!\n            } else {\n                return nullptr; // if right and left subtree dont have "max depth node" return null!\n            }\n        }\n    }\n\n    TreeNode* subtreeWithAllDeepest(TreeNode* root) {\n        int h = height(root);\n        int curh = 0;\n        TreeNode* ans = dfs(root, h, curh);\n        return ans;\n    }\n};\n\n```
['C++']