DFS
Complexity

Time complexity:
O(n)


Space complexity:
O(n)

Code
# Complexity
- Time complexity:
$$O(n)$$

- Space complexity:
$$O(n)$$

# Code
```typescript []
function subtreeWithAllDeepest(root: TreeNode | null): TreeNode | null {
    const dfs = (node: typeof root, depth: number): number => {
        if (!node) return depth;
        const left = dfs(node.left, ++depth);
        const right = dfs(node.right, depth);
        if (left === right && left > resDepth) {
            resDepth = left - 1, resHead = node;
        }
        return Math.max(left, right);
    };
    let resHead = root, resDepth = 0;
    dfs(root, 0);
    return resHead;
}
```
```javascript []
function subtreeWithAllDeepest(root) {
    const dfs = (node, depth) => {
        if (!node) return depth;
        const left = dfs(node.left, ++depth);
        const right = dfs(node.right, depth);
        if (left === right && left > resDepth) {
            resDepth = left - 1, resHead = node;
        }
        return Math.max(left, right);
    };
    let resHead = root, resDepth = 0;
    dfs(root, 0);
    return resHead;
}
```
['Tree', 'Depth-First Search', 'Recursion', 'Simulation', 'Binary Tree', 'TypeScript', 'JavaScript']