[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 binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
1
/ \
2 3
/ \
4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
给一个二叉树,计算二叉树的直径,直径是任意两个节点之间的最长路径。
解法1: traverse every node to get max of leftDepth, then rightDepth, then add those 2 at every node, calculate the max depth by helper. Complexity: Time O(N^2) Space O(N^2)
解法2:find out the max of leftDepth & rightDepth while at each node, meanwhile update the total max. Complexity: Time O(N) Space O(N)
最长路径有两种情况:
1. 最长条路径经过根节点,那么只需要找出根节点的左右两棵子树的最大深度然后相加即可。
2. 最长路径没有经过根节点,那么只需要找出根节点的左子树或者根节点的右子树作为根的最长路径度即可。递归调用,自底向上查找子树的深度,如果某一个左子树与右子树深度之和大于当前纪录的直径,那么替换为当前直径,递归完成之后即可找出直径。
Java:
public class Solution {
int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
if(root == null)
return 0;
max = Math.max(max, helper(root.left) + helper(root.right));
diameterOfBinaryTree(root.left);
diameterOfBinaryTree(root.right);
return max;
}
public int helper(TreeNode root){
if(root == null)
return 0;
return 1 + Math.max(helper(root.left), helper(root.right));
}
}
Java:
public class Solution {
int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
maxDepth(root);
return max;
}
public int maxDepth(TreeNode root){
if(root == null)return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
max = Math.max(max, left + right);
return 1 + Math.max(left, right);
}
}
Java:
class Solution {
int depth = 0;
public int diameterOfBinaryTree(TreeNode root) {
search(root);
return depth;
}
private int search(TreeNode root) {
if (root == null) {
return 0;
}
int left = search(root.left);
int right = search(root.right);
if (depth < left + right) {
depth = left + right;
}
return left > right ? left + 1 : right + 1;
}
}
Python:
class Solution():
def diameter(self, root):
if not root:
return 0
res = self.depth(root.left) + self.depth(root.right)
return max(res, max(self.diameter(root.left), self.diameter(root.right))) def depth(self, node):
if not node:
return 0
return 1 + max(self.depth(node.left), self.depth(node.right))
Python:
class Solution():
def __init__(self):
self.max = 0 def diameter(self, root):
self.helper(root)
return self.max def helper(self, root):
if not root:
return 0
left = self.helper(root.left)
right = self.helper(root.right)
self.max = max(self.max, left + right)
return 1 + max(left, right)
Python:
def height(node):
if node is None:
return 0 ;
return 1 + max(height(node.left), height(node.right)) def diameter(root):
if root is None:
return 0; lheight = height(root.left)
rheight = height(root.right) ldiameter = diameter(root.left)
rdiameter = diameter(root.right) return max(lheight + rheight, max(ldiameter, rdiameter))
Python:
class Solution(object):
def diameterOfBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def depth(root, diameter):
if not root: return 0, diameter
left, diameter = depth(root.left, diameter)
right, diameter = depth(root.right, diameter)
return 1 + max(left, right), max(diameter, 1 + left + right) return depth(root, 1)[1] - 1
C++: perfect solution , runtime = 26 ms
class Solution7 {
public:
int diameterOfBinaryTree(TreeNode *root) {
if (!root) return 0;
int res = depthOfNode(root->left) + depthOfNode(root->right);
return max(res, max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right)));
}
int depthOfNode(TreeNode *node) {
if (!node) return 0;
return max(depthOfNode(node->left), depthOfNode(node->right)) + 1;
}
};
类似题目:
[LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
All LeetCode Questions List 题目汇总
[LeetCode] 543. Diameter of Binary Tree 二叉树的直径的更多相关文章
- 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 ...
- [leetcode]543. Diameter of Binary Tree二叉树的直径
题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 ...
- [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 ...
- 543 Diameter of Binary Tree 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树 1 / \ 2 ...
- 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 ...
- 543. Diameter of Binary Tree 二叉树的最大直径
[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...
- Leetcode543.Diameter of Binary Tree二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它 ...
- [leetcode] 543. Diameter of Binary Tree (easy)
原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...
随机推荐
- The Tower(HDU6559+2018年吉林站+数学)
题目链接 传送门 题意 告诉你圆锥的底部圆的半径和圆锥的高,再给你一个点的坐标及其运动向量,问你这个点什么时候会与这个圆锥相撞. 思路 比赛场上二分一直没过但是有人二分过了,今天再写这题想再试下二分, ...
- linux卸载及安装mysql 5.7以上
删除: 1.rpm -qa|grep -i mysql 查看安装的mysql 2./usr/local/mysql/support-files/mysql.server stop 停止mys ...
- 《基于 UML 的教务系统设计方法研究》论文笔记(十五)
标题:基于 UML 的教务系统设计方法研究 时间:2009 来源:太原师范学院 关键词:UML:面向对象:建模:教务管理系统. 二.研究内容 UML 建模 UML 涵盖了面向对象的分析.设计和实现,融 ...
- 在Windows10系统下安装Oracle 11g数据库
在Windows10系统下安装Oracle 11g数据库 https://blog.csdn.net/wei1992_6/article/details/60054727
- pandas 筛选
t={ , , np.nan, , np.nan, ], "city": ["BeiJing", "ShangHai", "Gua ...
- STM32F103 串口-IAP程序升级
STM32F103 串口-IAP程序升级 通常情况下我们给STM32系列的单片机烧录程序文件的时候,使用SWD.J-link或者通过设置BOOT引脚后,使用串口进行程序下载,这样的方式直接一次性将程序 ...
- 题解 LA2889
题目大意 多组数据,每组数据给出一个正整数 \(n\),输出第 \(n\) 大的回文数(即 \(1,2,3,\cdots\)). 分析 不难发现,\(n\) 位的回文数有 \(9*10^{\lfloo ...
- 项目集成Spring Security
前言 之前写的 涂涂影院管理系统 这个 demo 是基于 shiro 来鉴权的,项目前后端分离后,显然集成 Spring Security 更加方便一些,毕竟,都用 Spring 了,权限管理当然 S ...
- Direction of Arrival Based Spatial Covariance Model for Blind Sound Source Separation
基于信号协方差模型DOA的盲声源分离[1]. 在此基础上,作者团队于2018年又发布了一篇文章,采用分级和时间差的空间协方差模型及非负矩阵分解的多通道盲声源分离[2]. 摘要 本文通过对短时傅立叶变换 ...
- 事件类型(onload)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...