题目地址https://leetcode-cn.com/problems/diameter-of-binary-tree/

递归+BFS(暴力解法)

我们可以考虑在每个节点时,都去计算该节点左子树和右子树的最大高度。这样会包含大量的重复计算在里面。时间复杂度O(n^2) 空间复杂度O(n)

class Solution {
public int diameterOfBinaryTree(TreeNode root) {
int result = 0;
Queue<TreeNode> queue = new LinkedList<>();
if (root == null) return result; queue.offer(root);
while(!queue.isEmpty()) {
TreeNode node = queue.poll();
int currMaxDepth = maxDepth(node.left) + maxDepth(node.right) + 1;
if (currMaxDepth > result)
result = currMaxDepth;
if (node.left != null)
queue.offer(node.left);
if (node.right != null)
queue.offer(node.right);
} return result - 1;
}
private int maxDepth(TreeNode node) {
if (node == null)
return 0;
return Math.max(maxDepth(node.left), maxDepth(node.right)) + 1;
}
}

2.递归+BFS(优化解法)

其实之前的maxDepth方法,已经是访问了所有节点的左子树和右子树的最大高度,这里我们只需要用个全局变量来缓存这个最大值即可,时间复杂度O(n) 空间复杂度O(h) h为树的最大深度

class Solution {
private int max;
public int diameterOfBinaryTree(TreeNode root) {
max = 1;
maxDepth(root);
return max - 1;
}
private int maxDepth(TreeNode node) {
if (node == null)
return 0;
int left = maxDepth(node.left);
int right = maxDepth(node.right);
max = Math.max(max, left + right + 1);
return Math.max(left, right) + 1;
}
}

更多LeetCode题解和数据结构方面的内容,可以关注我的github,求个star~ ▄█▔▉●

leetcode-0543 二叉树的直径的更多相关文章

  1. Leetcode 543.二叉树的直径

    二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, ...

  2. [LeetCode] 543. 二叉树的直径 ☆(递归、数最大深度)

    描述 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它的长 ...

  3. Java实现 LeetCode 543. 二叉树的直径(遍历树)

    543. 二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过也可能不穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / ...

  4. Java实现 LeetCode 543 二叉树的直径

    543. 二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 ...

  5. LeetCode 543. Diameter of Binary Tree (二叉树的直径)

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  6. [LeetCode] Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  7. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  8. Leetcode题目543:二叉树的直径(简单)

    题目描述: 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, ...

  9. LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)

    题目: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of ...

  10. [Swift]LeetCode543. 二叉树的直径 | Diameter of Binary Tree

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

随机推荐

  1. effective-java学习笔记---注解优于命名模式39

    命名模式的缺点有以下三点:(在第 4 版之前,JUnit 测试框架要求其用户通过以 test[Beck04] 开始名称来指定测试方法) 1.拼写错误导致失败,但不会提示. 2.无法确保它们仅用于适当的 ...

  2. TensorFlow Windows 安装

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 本系列教程将手把手带您从零开始学习Tensorflow,并最终通过 ...

  3. 累加数的贡献 CodeForces - 1213D2

    题意: 第一行输入n,k,表示有n个数,可以进行整除2操作,要是数组有k个相等的数,最少需要几次操作. 思路: 用一个数组记录每一个数出现的次数,如果一开始大于等于k,直接输出0,否则对这n个数进行从 ...

  4. POJ 1797 最短路变形所有路径最小边的最大值

    题意:卡车从路上经过,给出顶点 n , 边数 m,然后是a点到b点的权值w(a到b路段的承重),求卡车最重的重量是多少可以从上面经过. 思路:求所有路径中的最小的边的最大值.可以用迪杰斯特拉算法,只需 ...

  5. Springboot 事务注解--- @Transactional

    spring boot @Transactional事物处理    spring boot 添加事物使用 @Transactional注解 简单使用 在启动类上方添加 @EnableTransacti ...

  6. 性能优化之三:将Dottrace过程加入持续集成

    之前分享过一篇如何做接口性能分析的文章,但是整个分析过程有点繁琐,需要写一个控制台程序调用被测接口,再预热.启动dottrace追踪,最后才能得到我们想要的性能分析报告.如果有办法一键生成性能分析报告 ...

  7. Java并发基础02. 传统线程技术中的定时器技术

    传统线程技术中有个定时器,定时器的类是Timer,我们使用定时器的目的就是给它安排任务,让它在指定的时间完成任务.所以先来看一下Timer类中的方法(主要看常用的TimerTask()方法): 前面两 ...

  8. Spring的jdbcTemplate操作

    Spring的jdbcTemplate操作 dao层,使用jdbcTemplate Spring对不同的持久化层技术的支持,都进行了封装 JDBC Hibernate5.0 MyBatis JPA 相 ...

  9. Redis 笔记(四)—— SET 常用命令

    常用命令 命令 用例和描述 SADD SADD key item [item ...] —— 将一个或多个元素添加到集合中,返回添加的数量 SREM SREM key item [item ...] ...

  10. thinkphp后端开发ajax接口开发测试

    数据自动填充,Firefox的网络查看JSON数据,查错. 同时,用getLastSql函数查询,并且开启日志文件记录网页执行的全过程,还可以用thinkPHP内置的trace的跟踪. 谁有更好地方法 ...