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之装饰器的进阶
一.带参数的装饰器 (必须会) 针对不同的app的验证,比如:天猫和天猫超市,京东商城和京东超市 def wrapper_out(n): print(n) def wrapper(f): def in ...
- FaceBook 发布星际争霸最大 AI 数据集
简介 我们刚发布了最大的星际争霸:Brood War 重播数据集,有 65646 个游戏.完整的数据集经过压缩之后有 365 GB,1535 million 帧,和 496 million 操作动作. ...
- 从DeepNet到HRNet,这有一份深度学习“人体姿势估计”全指南
从DeepNet到HRNet,这有一份深度学习"人体姿势估计"全指南 几十年来,人体姿态估计(Human Pose estimation)在计算机视觉界备受关注.它是理解图像和视频 ...
- Typora+markdown 最常用语法教程
Typora+markdown 最常用语法教程(by 程序员宝藏) Typora+markdown 最常用语法教程(by 程序员宝藏) 请先配置推荐配置(文件->偏好设置): 文章目录 Typo ...
- php _weakup()反序列化漏洞
概念&原理 序列化就是使用 serialize() 将对象用字符串的方式进行表示: 反序列化是使用 unserialize() 将序列化的字符串构造成相应的对象,为序列化的逆过程. 序列化的对 ...
- java web综合案例
1.采用的技术: bootstrap+jsp+servlet+三层架构(servlet,service,dao)+mysql 注意:mysql使用的是5.5版本,使用高版本会有很多问题.可以将5.5版 ...
- HTML特殊转义字符——特殊符号
干货,见下图: 后期我会陆续更一些JavaScript的文章,大家可以一起学习交流.
- pycharm文件名颜色代表的含义
在使用pycharm过程中,文件名有不一样的颜色. 绿色:已经加入版本控制暂未提交 红色:未加入版本控制 蓝色:加入版本控制,已提交,有改动 白色:加入版本控制,已提交,无改动 灰色:版本控制已忽略文 ...
- 为应用程序池“XXX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误。该进程 ID 为“XXXX”。数据字段包含错误号。 改进查找流程
原文链接:https://www.cnblogs.com/qidian10/p/6028784.html 为防止原作者删除,留作解决方法备份 ---------------------------- ...
- Nginx 是如何处理 HTTP 头部的?
Nginx 处理 HTTP 头部的过程 Nginx 在处理 HTTP 请求之前,首先需要 Nginx 的框架先和客户端建立好连接,然后接收用户发来的 HTTP 的请求行,比如方法.URL 等,然后接收 ...