LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  int diameterOfBinaryTree(TreeNode* root) {
    int res = 0;
    depth(root, res);
    return res;
  }

  int depth(TreeNode* root, int& res) {
    if (!root) {
      return 0;
    }
    int l = depth(root->left, res);
    int r = depth(root->right, res);
    res = max(res, l + r);
    return 1 + max(l, r);
  }
};