Solution
C++ []\nclass Solution {\npublic:\n    int height(TreeNode* root) {\n        if (!root) return 0;\n        return max(height(root->left) + 1, height(root->right
```C++ []\nclass Solution {\npublic:\n    int height(TreeNode* root) {\n        if (!root) return 0;\n        return max(height(root->left) + 1, height(root->right) + 1); \n    }\n    TreeNode* subtreeWithAllDeepest(TreeNode* root) {\n        if (!root) return NULL;\n\n        int left = height(root->left);    \n        int right = height(root->right);\n\n        if (left == right) return root;\n        if (left > right) return subtreeWithAllDeepest(root->left);\n        return subtreeWithAllDeepest(root->right);\n    }\n};\n```\n\n```Python3 []\nclass Solution:\n    def subtreeWithAllDeepest(self, root: TreeNode) -> TreeNode:\n        found = False\n        ans = TreeNode(-1)\n        \n        def dfs(node,p,q):\n            nonlocal found, ans\n            if not node: return []\n            \n            left = dfs(node.left,left_node,right_node)\n            right = dfs(node.right,left_node,right_node)\n            \n            total = [*left,*right]\n            total.append(node.val)\n            \n            if p.val in total and q.val in total and not found: \n                ans = (node)\n                found = True\n            \n            return total\n\n        def find_depth(root):\n            q = deque()\n            q.append(root)\n\n            while q:\n                l,r = None,None\n                no_nodes = len(q)\n                for i in range(len(q)):\n                    a = q.popleft()\n                    if i == 0: l = a\n                    if i == no_nodes-1: r = a\n\n                    if a.left: q.append(a.left)\n                    if a.right: q.append(a.right)\n            \n            return l,r\n        \n        left_node,right_node = find_depth(root)\n        dfs(root,left_node,right_node)\n        \n        return ans\n```\n\n```Java []\nclass Solution {\n    int maxDepth = Integer.MIN_VALUE;\n    TreeNode result = null;\n    public TreeNode subtreeWithAllDeepest(TreeNode root) {\n        postOrder(root, 0);\n        return result;\n    }\n    public int postOrder(TreeNode node, int deep) {\n        if (node == null) {\n            return deep;\n        }\n        int left = postOrder(node.left, deep + 1);\n        int right = postOrder(node.right, deep + 1);\n        int curDepth = Math.max(left, right);\n        maxDepth = Math.max(maxDepth, curDepth);\n        if (left == maxDepth && right == maxDepth) {\n            result = node;\n        }\n        return curDepth;\n    }\n}\n```\n
['C++', 'Java', 'Python3']