543. 二叉树的直径
C代码:二叉树
// 遍历每个节点、取两个节点的边数和给max;return每个节点的最大边
int max;int dfs(struct TreeNode* root) {if (root == NULL) {return 0;}int left = dfs(root->left);int right = dfs(root->right);max = fmax(max, left + right);return fmax(left, right) + 1; // 返回最大边数给每个节点
}int diameterOfBinaryTree(struct TreeNode* root){max = 0;dfs(root);return max;
}