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 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.
分析:
给定一棵二叉树,计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。
我们定义空结点的直径长度为0,而除root结点外,其余所有结点都有一条边连接其父结点。那么递归求解此问题,当前结点所有的直径长度等于其左右孩子的长度之和,也就是把当前结点当成桥接两个孩子结点的桥梁,更新全局的最大值,但如果当前结点还有父结点的话,则应该是左右孩子的长度取最大值加1,1也就是当前结点连向父结点的那条边,而由于题目规定,我们只能通过一次结点,所以取最大值就好。
程序:
C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
int res = ;
dTree(root, res);
return res;
}
private:
int dTree(TreeNode* root, int& res){
if(root == nullptr)
return ;
int l = dTree(root->left, res);
int r = dTree(root->right, res);
res = max(res, l+r);
return max(l, r) + ;
}
};
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int diameterOfBinaryTree(TreeNode root) {
res = 0;
dTree(root);
return res;
}
private int dTree(TreeNode root){
if(root == null)
return 0;
int l = dTree(root.left);
int r = dTree(root.right);
res = Math.max(res, l+r);
return Math.max(l, r) + 1;
}
private int res;
}
LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)的更多相关文章
- [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. 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 ...
随机推荐
- docker安装并设置开机启动(Linux)
docker 开机启动: systemctl enable docker 使用的linux系统为CentOS7.2 docker分为CE和EE版本,EE版本收费,一般我们使用CE版本就满足要求了 do ...
- awk&sed
sed BRE awk ERE sed 不能采用? awk可以 sed 在匹配的任何时候可以用^,$ awk必须除了在行头和行尾 其他地方必须转义
- UVA 1601 双向BFS
但是我们还不是很清楚每一次的状态怎么储存?我们可以用一个结构体,将每次的位置存起来,但是这个程序中用了一个更好的储存方法:我们知道最大的格数是16*16个,也就是256个,那么我们转换为二进制表示就是 ...
- [BJDCTF2020]Cookie is so stable
0x00 知识点 Twig模板注入 链接: https://www.k0rz3n.com/2018/11/12/%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0%E5%B8%A ...
- Linux学习《第四章脚本》20200222
- 关于安装openfiler
简介 Openfiler 由rPath Linux驱动,它是一个基于浏览器的免费网络存储管理实用程序,可以在单一框架中提供基于文件的网络连接存储 (NAS) 和基于块的存储区域网 (SAN).Open ...
- 用CSS编写多种常见的图形
用CSS编写多种常见的图形 正方形与长方形 这个是最简单的,直接上代码 <!DOCTYPE html> <html> <head> <title>< ...
- 第二阶段scrum-10
1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 等待发布
- Maven与nexus关系
一.了解Maven,Maven用来干什么呢 1. 优秀的构建工具 通过简单的命令,能够完成清理.编译.测试.打包.部署等一系列过程.同时,不得不提的是,Maven是跨平台的,无论是在Windows.还 ...
- 修改电脑IP地址和MAC地址
一.修改IP地址: 电脑右下角:上网的图标,点击右键,打开“网络和共享中心”, 点击“本地连接”,打开的窗口点击“属性”, 打开新窗口,找到“IPv4”,点击“属性”, 打开新窗口,修改ip,保存,关 ...