mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
add 543. Diameter of Binary Tree 🍺
This commit is contained in:
parent
091918466d
commit
b793bc4df9
@ -313,6 +313,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。
|
|||||||
| 500 |[Keyboard Row](https://leetcode.com/problems/keyboard-row)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/500.%20Keyboard%20Row.md)|Easy| |
|
| 500 |[Keyboard Row](https://leetcode.com/problems/keyboard-row)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/500.%20Keyboard%20Row.md)|Easy| |
|
||||||
| 509 |[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)|[C++](solutions/509.%20Fibonacci%20Number.md)|Easy| |
|
| 509 |[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)|[C++](solutions/509.%20Fibonacci%20Number.md)|Easy| |
|
||||||
| 532 |[K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/532.%20K-diff%20Pairs%20in%20an%20Array.md)|Easy| |
|
| 532 |[K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/532.%20K-diff%20Pairs%20in%20an%20Array.md)|Easy| |
|
||||||
|
| 543 |[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[C++](solutions/543.%20Diameter%20of%20Binary%20Tree.md)|Easy| |
|
||||||
| 561 |[Array Partition I](https://leetcode.com/problems/array-partition-i)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/561.%20Array%20Partition%20I.md)|Easy| |
|
| 561 |[Array Partition I](https://leetcode.com/problems/array-partition-i)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/561.%20Array%20Partition%20I.md)|Easy| |
|
||||||
| 566 |[Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/566.%20Reshape%20the%20Matrix.md)|Easy| |
|
| 566 |[Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/566.%20Reshape%20the%20Matrix.md)|Easy| |
|
||||||
| 572 |[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)|[C++](solutions/572.%20Subtree%20of%20Another%20Tree.md)|Easy| |
|
| 572 |[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)|[C++](solutions/572.%20Subtree%20of%20Another%20Tree.md)|Easy| |
|
||||||
|
28
solutions/543. Diameter of Binary Tree.md
Normal file
28
solutions/543. Diameter of Binary Tree.md
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# [543. Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)
|
||||||
|
|
||||||
|
# 思路
|
||||||
|
|
||||||
|
求二叉树的直径,所谓直径就是两点之间的最远距离。对于树中的每个节点,我们都可以计算出经过该节点的两点间的最远距离(就等于其左子树的高加上右子树的高)。计算左右子树的高就是一个中序遍历。
|
||||||
|
|
||||||
|
时空复杂度均为O(n)
|
||||||
|
|
||||||
|
|
||||||
|
# C++
|
||||||
|
``` C++
|
||||||
|
class Solution {
|
||||||
|
private:
|
||||||
|
int res = 0;
|
||||||
|
int inorder(TreeNode *root){
|
||||||
|
if(!root) return 0;
|
||||||
|
int l = inorder(root -> left);
|
||||||
|
int r = inorder(root -> right);
|
||||||
|
res = max(res, l + r);
|
||||||
|
return 1 + max(l, r);
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
int diameterOfBinaryTree(TreeNode* root) {
|
||||||
|
inorder(root);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user