当前位置: 代码迷 >> 综合 >> [leetcode] 543. Diameter of Binary Tree (easy)
  详细解决方案

[leetcode] 543. Diameter of Binary Tree (easy)

热度:25   发布时间:2024-01-05 01:13:52.0
原题

思路:
题目其实就是求左右最长深度的和

class Solution
{private:int res = 0;public:int diameterOfBinaryTree(TreeNode *root){dfs(root);return res;}int dfs(TreeNode *root){if (root == NULL){return 0;}int leftNum = dfs(root->left);int rightNum = dfs(root->right);res = max(res, leftNum + rightNum);return max(leftNum, rightNum) + 1;}
};
  相关解决方案