当前位置: 首页> 科技> IT业 > 陕西工程项目信息网_html中文模板_一键关键词优化_做好的网站怎么优化

陕西工程项目信息网_html中文模板_一键关键词优化_做好的网站怎么优化

时间:2025/7/10 17:22:40来源:https://blog.csdn.net/taoqick/article/details/144171303 浏览次数:0次
陕西工程项目信息网_html中文模板_一键关键词优化_做好的网站怎么优化

Given the root of a binary tree, return 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.

The length of a path between two nodes is represented by the number of edges between them.

Example 1:
在这里插入图片描述
Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
Example 2:

Input: root = [1,2]
Output: 1

Constraints:

The number of nodes in the tree is in the range [1, 104].
-100 <= Node.val <= 100


use nonlocal

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:def dfs(root):nonlocal max_lenif root == None:return 0l = dfs(root.left)r = dfs(root.right)max_len = max(max_len, l+r)return max(l,r)+1max_len = 0dfs(root)return max_len
关键字:陕西工程项目信息网_html中文模板_一键关键词优化_做好的网站怎么优化

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: