leetcode-0543 二叉树的直径
题目地址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 二叉树的直径的更多相关文章
- Leetcode 543.二叉树的直径
二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, ...
- [LeetCode] 543. 二叉树的直径 ☆(递归、数最大深度)
描述 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它的长 ...
- Java实现 LeetCode 543. 二叉树的直径(遍历树)
543. 二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过也可能不穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / ...
- Java实现 LeetCode 543 二叉树的直径
543. 二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 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 ...
- [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 ...
- [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 ...
- Leetcode题目543:二叉树的直径(简单)
题目描述: 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, ...
- 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 ...
- [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 ...
随机推荐
- 从零开始实现穿衣图像分割完整教程(附python代码演练)
时装业是人工智能领域很有前景的领域.研究人员可以开发具有一定实用价值的应用.我已经在这里展示了我对这个领域的兴趣,在那里我开发了一个来自Zalando在线商店的推荐和标记服装的解决方案. 在这篇文章中 ...
- Python python 五种数据类型--字符串
# python 字符串的初始化 var1 = 'hello,world' # python 字符串为不可变类型 var2= var1* 2 print(var1) #hello,world prin ...
- jsonp跨域的原理及实现
1,什么是跨域? 跨域跨域,跨过域名,笼统来说就是一个域名区请求另外一个域名的数据,但实际上,不同端口.不同域名.不同协议上请求数据都会出现跨域问题.浏览器出于安全考虑会报出异常,拒绝访问. 2,js ...
- 来讨论一下这些常见的 Redis 面试题
Redis应该算面试中必问的一个知识点,但是发现很多童鞋并不熟悉这块,这篇就常见的一些问题做一些整理,有不对的地方欢迎留言指正! 1.Redis支持的数据类型? String(字符串) 格式: set ...
- 十进制转化为非十进制C++代码
还是先为大家介绍一下原理吧. 假设余数为 r ,十进制数为 n :(拆分为整数 zs ,余数 ys) 对 zs:需要将 zs 除 r 取余数,直到商为 0 停止,将余数倒序排列即可. 对 ys:需要将 ...
- ML-Agents(六)Tennis
目录 ML-Agents(六)Tennis 一.Tennis介绍 二.环境与训练参数 三.场景基本结构 四.代码分析 环境初始化脚本 Agent脚本 Agent初始化与重置 矢量观测空间 Agent动 ...
- .net core 跨平台开发 微服务架构 基于Nginx反向代理 服务集群负载均衡
1.概述 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客 ...
- MTK Android [输入法]客制化系统默认输入法-搜狗输入法
1.frameworks/base/packages/SettingsProvider/res/values/defaults.xml <!--Sogou input method is use ...
- (js描述的)数据结构[哈希表1.2](9)
一. 优秀的哈希函数 1.快速的计算: 需要快速的计算来获得对应的hashCode(霍纳法则来减少乘除次数) 2.均匀的分布: 尽可能将元素映射到不同的位置,让元素在哈希表中均匀分布 二.哈希表的扩容 ...
- IO操作与IO模型
目录 一 .IO操作本质 二. IO模型 BIO – 阻塞模式I/O NIO – 非阻塞模式I/O IO Multiplexing - I/O多路复用模型 AIO – 异步I/O模型 三.同步I/O与 ...