class Solution {
int ans=0;
public int diameterOfBinaryTree(TreeNode root) {
if(root == null) return 0;
depth(root);
return ans;
}
public int depth(TreeNode node){
if(node == null) return 0;
int left = depth(node.left);
int right = depth(node.right);
ans= Math.max(ans,left+right);
return Math.max(left,right)+1;
}
}
class Solution {
public:
int result = 0;
int depth(TreeNode* root){
if(root == NULL) return 0;
int left = depth(root->left);
int right = depth(root->right);
result = max(left+right, result);
return max(left, right) +1;
}
int diameterOfBinaryTree(TreeNode* root) {
depth(root);
return result;
}
};
class Solution { // 中序
int res = 0;
public int diameterOfBinaryTree(TreeNode root) {
solve(root);
return res;
}
int solve(TreeNode root){
if(root == null){
return 0;
}
int left = solve(root.left);
int right = solve(root.right);
res = Math.max(left + right, res);
return Math.max(left, right) + 1;
}
class Solution {
int res;
int depth(TreeNode* root){
if (root == NULL) {
return 0;
}
int L = depth(root->left);
int R = depth(root->right);
res = max(res, L + R);
return max(L, R) + 1;
}
public:
int diameterOfBinaryTree(TreeNode* root) {
depth(root);
return res;
}
};
class Solution {
// 定义全局变量,记录树的最长节点数
int ans;
public int diameterOfBinaryTree(TreeNode root) {
ans = 1;
depth(root); // 数的最长节点数
return ans - 1; // 树的边长等于节点数-1
}
public int depth(TreeNode root) {
if (root == null) return 0;
int L= depth(root.left); // 左子树的最长节点
int R = depth(root.right); // 右子树的最长节点
ans = Math.max(ans, L + R + 1); // 更新整个树的最长节点
return Math.max(L, R) + 1; // 返回包含单一子树的最长节点
}
}
思路:在求最大深度的基础下,加上max全局变量,遍历每个结点的左右分支深度和的最大值即为最大直径(也就是最长的最短路径)
class Solution {
int max=1;
public int diameterOfBinaryTree(TreeNode root){
depth(root);
return max-1;
}
public int depth(TreeNode root){
if(root==null) return 0;
int depthL=depth(root.left);
int depthR=depth(root.right);
int res=Math.max(depthR,depthL)+1;
max=Math.max(max,depthL+depthR+1);
return res;
}
}
时间复杂度:o(n)
空间复杂度:o(n)
class Solution {
public int ans = 0;
public int diameterOfBinaryTree(TreeNode root) {
depth(root);
return ans;
}
public int depth(TreeNode root) {
if (root == null)
return 0;
int left_depth = depth(root.left);
int right_depth = depth(root.right);
ans = Math.max(ans, left_depth + right_depth);
return Math.max(left_depth, right_depth) + 1;
}
}
class Solution {
static int ans;
public int diameterOfBinaryTree(TreeNode root) {
ans = 0;
depth(root);
return ans;
}
public int depth(TreeNode root) {
if (root == null) {
return 0;
}
int left = depth(root.left);
int right = depth(root.right);
// 经过某一个节点的最长路径 = 左树深度 + 右树深度
ans = Math.max(left + right, ans);
return Math.max(left, right) + 1;
}
}
共以下 16 个回答
543. 二叉树的直径
实际上相当于从根节点左子树最深处走到根节点再到右子树最深点,即result=左树高+右树高
解题思路
– 某一个节点作为顶点时候的路径(直径)= 左子树的高度+右子树的高度
– 只需要新建1个求高度的函数,1个遍历全部节点的递归过程中(把全局变量值直径记录下来)
【题解1】不建议,展示的是最直观的思路
【题解2】建议,改进点就是在计算二叉树的深度时,(顺便借助这个遍历)去求出直径,这样就不需要二次遍历了
思路:对左右子树分别递归,注意求和的时候深度要记得+1才包含了子树到父节点的那个深度
class Solution { // 中序
int res = 0;
public int diameterOfBinaryTree(TreeNode root) {
solve(root);
return res;
}
}
时间:O(n)
空间:O(n)
经过当前节点的直径为其左子树深度与右子树深度相加。可以在递归求解二叉树深度时,定义全局变量保存直径的最大值。
时间复杂度O(N),空间复杂度O(N)。
直径就是左右深度相加
on on
时间复杂度O(n),空间复杂度O(h)
时间复杂度:O(n)
空间复杂度:O(n)
LCR 175. 计算二叉树的深度 – 力扣(LeetCode)的变形,主要是理解题意:经过某一个节点的最长路径 = 左树深度 + 右树深度
思路: 计算左右子树直径
思路:一个方法是根据二叉树的左右子树的最大高度进行计算,一个是根据左右子树到当前节点的最长链长进行计算。
时间复杂度:O(n)
空间复杂度:O(n)
时间o(n)
空间o(n)
//递归遍历二叉树
class Solution {
private int maxDiameter = 0;
public int diameterOfBinaryTree(TreeNode root) {
depth(root);
return maxDiameter;
}
}