题目如下:

解题思路:最长的周长一定是树中某一个节点(不一定是根节点)的左右子树中的两个叶子节点之间的距离,所以最简单的办法就是把树中所有节点的左右子树中最大的两个叶子节点之间的距离求出来,最终得到最大值。

代码如下:

# 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):
res = 0
def getLargestDis(self,node,distance):
if node == None:
return distance-1
else:
return max(self.getLargestDis(node.left,distance+1),self.getLargestDis(node.right,distance+1))
def traverse(self,node):
distance = 1
self.res = max(self.res, self.getLargestDis(node.left,distance) + self.getLargestDis(node.right,distance))
if node.left != None:
self.traverse(node.left)
if node.right != None:
self.traverse(node.right) def diameterOfBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return 0
self.res = 0
self.traverse(root)
return self.res

【leetcode】543. Diameter of Binary Tree的更多相关文章

  1. 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  2. 【leetcode_easy】543. Diameter of Binary Tree

    problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  5. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  6. 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  7. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  8. 【leetcode】Minimum Depth of Binary Tree (easy)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

    Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and ...

随机推荐

  1. 添加对象到 HashSet 里的规则是

    下面的解释取自百度知道的一位网友的回答,链接如下: java HashSet类添加元素的问题_百度知道http://zhidao.baidu.com/link?url=9bcAnolev1EBeFI_ ...

  2. html from表单异步处理

    from表单异步处理. 简单处理方法: jQuery做异步提交表单处理, 通过$("#form").serialize()将表单元素的数据转化为字符串, 最后通过$.ajax()执 ...

  3. python两个装饰器的运算顺序

    #装饰顺序按靠近函数顺序执行,调用时由外而内,执行顺序和装饰顺序相反. def makebold(func): def wrap(): return "<i>"+fun ...

  4. (转)pycharm autopep8配置

    转:https://blog.csdn.net/BobYuan888/article/details/81943808 1.pip下载安装: 在命令行下输入以下命令安装autopep8 pip ins ...

  5. MySql使用mysqldump 导入与导出方法总结

    导出数据库数据:首先打开cmd进入MySQL的bin文件夹下 1.导出education数据库里面的users表的表数据和表结构(下面以users表为例) mysqldump -u[用户名] -h[i ...

  6. ruby+selenium-webdriver测试

    参考这里的博客https://www.cnblogs.com/smiling007/p/5116662.html

  7. log4j配置及异常、解决办法

    配置: ### set log levels ### D只有一个E也只有一个 log4j.rootLogger = debug,stdout,D,E ### 输出到控制台 ### log4j.appe ...

  8. Java 遍历某个目录

    import java.io.File; import java.io.IOException; public class DirErgodic { public static void find(S ...

  9. P5030 长脖子鹿放置

    题目背景 众周所知,在西洋棋中,我们有城堡.骑士.皇后.主教和长脖子鹿. 题目描述 如图所示,西洋棋的"长脖子鹿",类似于中国象棋的马,但按照"目"字攻击,且没 ...

  10. Python : Polymorphism

    class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): ...