题目描述
Given the root
of a binary tree, return the length of the diameter of the tree.
The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root
.
The length of a path between two nodes is represented by the number of edges between them.
Example 1:
Input: root = [1,2,3,4,5] Output: 3 Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
Example 2:
Input: root = [1,2] Output: 1
Constraints:
- The number of nodes in the tree is in the range
[1, 10^4]
. -100 <= Node.val <= 100
解题思路
【C++解法】
/**
* 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 mNodesOfLongestPath = 0;
int diameterOfBinaryTree(TreeNode* root) {
diameter(root);
return mNodesOfLongestPath - 1;
}
int diameter(TreeNode* root) {
if (root == nullptr) {return 0;}
int left = diameter(root->left) + 1;
int right = diameter(root->right) + 1;
mNodesOfLongestPath = max(left + right - 1, mNodesOfLongestPath);
return max(left, right);
}
};
用pair对象返回参数:
class Solution {
public:
pair<int, int> diameter(TreeNode* root) {
if (root == nullptr) {return make_pair(0, 0);}
pair<int, int> left = diameter(root->left);
pair<int, int> right = diameter(root->right);
return make_pair(max(left.first, right.first) + 1,
max((left.first + right.first),
max(left.second, right.second)));
}
int diameterOfBinaryTree(TreeNode* root) {
return diameter(root).second;
}
};
引用参数带一个,返回值带一个:
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
int diameter = 0;
helper(root, diameter);
return diameter;
}
int helper(TreeNode* node, int& diameter) {
if (!node) {
return 0;
}
int l = helper(node->left, diameter);
int r = helper(node->right, diameter);
diameter = max(l + r, diameter);
return max(l, r) + 1;
}
};
【Java解法】
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
使用全局变量:
class Solution {
int mNodesOfLongestPath = 0;
public int diameterOfBinaryTree(TreeNode root) {
diameter(root);
return mNodesOfLongestPath - 1;
}
public int diameter(TreeNode root){
if(root == null) return 0;
int left = diameter(root.left) + 1;
int right = diameter(root.right) + 1;
mNodesOfLongestPath = Math.max(left + right - 1, mNodesOfLongestPath);
return Math.max(left, right);
}
}
用pair对象返回参数:
class Solution {
class pair{
int height;
int diameter;
pair(int height,int diameter){
this.height = height;
this.diameter = diameter;
}
}
public pair diameter(TreeNode node){
if (node == null) {return new pair(0,0);}
pair left = diameter(node.left);
pair right = diameter(node.right);
return new pair(Math.max(left.height, right.height) + 1,
Math.max((left.height + right.height),
Math.max(left.diameter, right.diameter)));
}
public int diameterOfBinaryTree(TreeNode root) {
return diameter(root).diameter;
}
}
参考文献