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 ...
随机推荐
- 最小生成树(次小生成树)(最小生成树不唯一) 模板:Kruskal算法和 Prim算法
Kruskal模板:按照边权排序,开始从最小边生成树 #include<algorithm> #include<stdio.h> #include<string.h> ...
- Thread ---java 内存模型
1,Java 线程之间的通信由Java 内存模型(JMM)控制.JMM决定一个线程对共享变量的写入时,能对另一个线程可见. 从抽象的角度来看,JMM定义了线程和主内存之间的抽象关系: 线程之间的共享变 ...
- mysql的Ft_hints: no_ranking
是不是发现找遍全网也没有找到相关资料? 巧了,我也是,所以我这里来进行一次大胆分析(基本靠猜) 在使用mysql的fulltext索引(全文索引)时,使用explain则会在extra中出现这句提示: ...
- JSP学习笔记(二)
JSP内置对象 request对象 response对象 session对象 application对象 out对象 有些对象不用声明就可以在JSP页面的Java程序片和表达式部分使用,这就是JSP的 ...
- dome 模块 pyaudio 声音处理 为语音识别准备
dome 模块 pyaudio 声音处理 为语音识别准备 直接上例子 dome1 声音强度检查 import pyaudio import numpy as np class QAudio: CHUN ...
- Codeforces 1322C - Instant Noodles(数学)
题目链接 题意 给出一个二分图, 两边各 n 个点, 共 m 条边, n, m ≤ 5e5. 右边的点具有权值 \(c_i\), 对于一个只包含左边的点的点集 S, 定义 N(S) 为所有与这个点集相 ...
- 解决ASP.NET MVC返回的JsonResult 中 日期类型数据格式问题,和返回的属性名称转为“驼峰命名法”和循环引用问题
DateTime类型数据格式问题 问题 在使用ASP.NET MVC 在写项目的时候发现,返回给前端的JSON数据,日期类型是 Date(121454578784541) 的格式,需要前端来转换一下才 ...
- 用全站 CDN 部署 Discourse 论坛
Discourse 介绍 Discourse 是一款由 Stack Overflow 的联合创始人--Jeff Atwood,基于 Ruby on Rails 开发的开源论坛.相较于传统论坛,Disc ...
- 基于 HTML5 WebGL 的楼宇智能化集成系统(二)
前言 一套完整的可视化操作交互上,必不可少 2D/3D 的融合,在上期我们介绍了有关 3D 场景的环视漫游.巡视漫游以及动画效果,还包括了冷站场景.热站场景以及智慧末端的实现原理,本期主要 ...
- ScrollViewer - 可用鼠标拖动滚动的列表框
ScrollViewer添加附加属性: using System; using System.Collections.Generic; using System.Windows; using Syst ...