-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path543.二叉树的直径.cpp
39 lines (35 loc) · 999 Bytes
/
543.二叉树的直径.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* @lc app=leetcode.cn id=543 lang=cpp
*
* [543] 二叉树的直径
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDiameter = 0;
int diameterOfBinaryTree(TreeNode* root) {
maxDepth(root);
return maxDiameter;
}
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
int left_max = maxDepth(root->left);
int right_max = maxDepth(root->right);
// 后序位置,顺便计算最大直径
int my_diameter = left_max + right_max;
maxDiameter = max(maxDiameter, my_diameter);
return 1 + max(left_max, right_max);
}
};
// @lc code=end