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 ...
随机推荐
- Android对话框(Dialog)
Android对话框 前几天出差没有进行更新,今天写一下安卓中用的比较多的对话框——AlertDialog. dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一 ...
- 学习GAN必须阅读的10篇论文
本文转载自:魔图互联.欢迎访问网站查看详细教程:Tensorflow(pytorch)系列教程 生成对抗网络是深度学习中最有趣和最受欢迎的应用之一.本文将列出 10 篇关于 GAN 的论文,这些论文详 ...
- OI滚粗记
1) 第一次接触OI是在初一,现在算下都四年了,最开始还是用的Pascal,而现在,Pascal都快被淘汰了.四年的OI生涯让我领会了很多,虽然失去了很多,但是也收获了少,这场名叫OI的生活,我不后悔 ...
- 【API知识】SpringBoot项目中@EnableXXX的原理
@EnableXX注解的使用场景 SpringBoot为开发人员提供了很多便利,例如如果想要定时功能,只要添加@EnableSchedule,即可配合@Schedule注解实现定时任务功能,不需要额外 ...
- coderforces Gym 100803A/Aizu 1345/CSU 1536/UVALive 6832 Bit String Reordering(贪心证明缺)
Portal: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1345 http://codeforces.com/gym/100 ...
- Python学习-第三节part1: 关于函数
一 为何要用函数之不用函数的问题 #1.代码的组织结构不清晰,可读性差 #2.遇到重复的功能只能重复编写实现代码,代码冗余 #3.功能需要扩展时,需要找出所有实现该功能的地方修改之,无法统一管理且维护 ...
- 新建基于STM32F103ZET6的工程-寄存器版本
1.新建空白工程 新建一个文件夹,用来存放新建的工程文件.在这个新建的文件夹下创建MDK-PRO和OUTPUT两个子文件夹.MDK-PRO文件夹用来存放工程文件:OUTPUT用来存放工程编译器后输出的 ...
- js 数组 随机排序
方法一: function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min) } fu ...
- 数据挖掘-K-近邻算法
数据挖掘-K-近邻算法 目录 数据挖掘-K-近邻算法 1. K-近邻算法概述 1.1 K-近邻算法介绍 1.1.1 KNN算法作用 1.1.2 KNN 算法思想 1.1.3 KNN算法特点 1.2 K ...
- Java 使用InputStream笔记
当我们要从网络下载资源时,使用类似如下方法来获取InputStream实例: URLConnection connection = new URL("http://www.XXXX.XXX& ...