[LeetCode&Python] Problem 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.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def diameterOfBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.ans=1
def dfs(node):
if not node:
return 0
L=dfs(node.left)
R=dfs(node.right)
self.ans=max(self.ans,L+R+1)
return max(L,R)+1
dfs(root)
return self.ans-1
[LeetCode&Python] Problem 543. Diameter of Binary Tree的更多相关文章
- 【leetcode_easy】543. Diameter of Binary Tree
problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- [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 (二叉树的直径)
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. 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&Python] Problem 669. Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...
- 【leetcode】543. Diameter of Binary Tree
题目如下: 解题思路:最长的周长一定是树中某一个节点(不一定是根节点)的左右子树中的两个叶子节点之间的距离,所以最简单的办法就是把树中所有节点的左右子树中最大的两个叶子节点之间的距离求出来,最终得到最 ...
随机推荐
- 开发Web应用(1)(二十)
静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /s ...
- python2和python3的区别总结
python2x和python3x区别: python2x:源码重复,不规范. python3x: 源码规范,优美,清晰,简单. 编译型:将代码一次性全部转化成字节码. 代表语言:C,C++ 优点: ...
- Grafana展示報表數據的配置(二)
一.Grafana以圖表的形式展示KPI報表的結果數據1.按照日期顯示數據達標量與未達標量2.顯示當前報表的最大值.最小值.平均值.總量3.報表結果數據的鏈接分享與頁面嵌入,用戶無需登錄直接訪問報表統 ...
- caffe,Inception v2 Check failed: top_shape[j] == bottom[i]->shape(j)
使用Caffe 跑 Google 的Inception V2 对输入图片的shape有要求,某些shape输进去可能会报错. Inception model中有从conv和pooling层concat ...
- 一个表中多个字段对应另一个表的ID(SQL查询)
A数据表中多个字段对应B数据表的ID, 现在要把B表的其他字段一起查询出来 一.数据表: 1.SPEED_DETECTION_ROAD 它的 START_POINT_ID 和 END_POINT_ID ...
- C++关于运算符的注意事项
1.函数调用也是一种特殊的运算符,对运算对象的个数不作限制. 2.几元运算符,是基于作用的对象的数量. 3.不同类型的运算对象进行运算,可能会出现类型转换,一般情况下小整数类型会被转换成较大的整数类型 ...
- Cracking The Coding Interview4.5
//原文: // // Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in ...
- rap使用手册
https://github.com/thx/RAP/wiki/user_manual_cn
- 《软件调试 Windows概要》
操作系统是计算机系统中的基本软件.它负责管理系统中的软硬件资源.通常都包括文件管理.内存管理.进程管理.打印管理.网络管理等基本功能.除此之外,支持调试也是操作系统设计的一项根本任务. 0x01 进 ...
- DevExpress WinForms使用教程:WinForms Sunburst控件
[DevExpress WinForms v18.2下载] DevExpress WinForms v18.2中包含了一个新的WinForms组件 - WinForms Sunburst,它旨在帮助开 ...