C# O(n) solution
\n\nWhen length of matrix is odd, the middle element is in both primary diagonal and secondary diagonal, so we only add this element once.\n\n\npublic class Sol
![image](https://assets.leetcode.com/users/images/495c06e0-bc46-4c73-840d-b4acf0e6ddcf_1600584231.9372227.png)\n\nWhen length of matrix is odd, the middle element is in both primary diagonal and secondary diagonal, so we only add this element once.\n\n```\npublic class Solution {\n    public int DiagonalSum(int[][] mat) {\n        \n        int sum = 0, n = mat.Length;\n        \n        for(int i = 0; i < n; i++)\n        {\n            sum += mat[i][i] + mat[i][n - 1 - i];\n        }\n        \n        return n % 2 == 0? sum : sum - mat[n / 2][n / 2];\n    }\n}\n```\n
[]