https://leetcode.com/problems/diameter-of-binary-tree/#/description

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. 1
  2. / \
  3. 2 3
  4. / \
  5. 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.

Hint:

Answer = max (max left_depth + max_right_depth, max left_depth, max right_depth)

The "max left_depth + max_right_depth" holds true when the right and left subtrees exist, and "max left_depth" holds true when only left subtree exists; likwise, "max right_depth" holds true when only right subtree exists. 

Back-up knowledge:

Q: How to calculate the depth of tree?

A:

1) Traverse the depth of left subtree.

2) Traverse the depth of right subtree.

3) Compare the two depths.  

If left_depth > right_depth, then return left_depth + 1.

else left_depth < right_depth, then return right_depth + 1.

P.S. The reason why we + 1 is that the root is at level 1. 

  1. class Solution:
  2. def TreeDepth(self, node):
  3. if node == None:
  4. return 0
  5. left_depth = Solution.TreeDepth(self, node.left)
  6. right_depth = Solution.TreeDepth(self, node.right)
  7. return max(left_depth, right_depth) + 1

Note:

1 It is implemented in a recursive manner. 

Sol:

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution(object):
  9. def diameterOfBinaryTree(self, root):
  10. """
  11. :type root: TreeNode
  12. :rtype: int
  13. """
  14. self.max_len = 0
  15. def depth(node):
  16. if not node:
  17. return 0
  18. left_depth = depth(node.left)
  19. right_depth = depth(node.right)
  20. self.max_len = max(self.max_len, left_depth + right_depth)
  21. return max(left_depth, right_depth) + 1
  22. depth(root)
  23. return self.max_len

Note:

1 Implant the idea of recursion in your head.  Don't think of "trace back".

2 self.max_len is a customer-defined variable/method. It's like "global variable", otherwise max_len can not carry the value in def depth to def diameterOfBinaryTree.  

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 ...

  2. 【leetcode_easy】543. Diameter of Binary Tree

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

  3. 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 ...

  4. [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 b ...

  5. [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 ...

  6. 543. Diameter of Binary Tree 二叉树的最大直径

    [抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...

  7. 543. Diameter of Binary Tree【Easy】【二叉树的直径】

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. struts2 之 struts2数据处理

    开门见山,struts2的数据处理总结: 1. 在servlet中,如果要获取页面提交的数据要使用requerst.getParameter方法来获取,并且每次需要进行相关的类型转换工作,数据的获取及 ...

  2. 求解释一个蛋疼的bug

     大婶儿们出来解决个问题,看看有碰见过的没 截图中的 if (order.EShopOrder_PayStatus == 0 && order.EShopOrder_Status == ...

  3. StringBuilder的实现

    先看看MS给出的官方解释吧 (http://msdn.microsoft.com/zh-cn/library/system.text.stringbuilder(VS.80).aspx) String ...

  4. R语言与SQL server链接

    第一步:创建数据源(方法见下面链接) http://www.2cto.com/database/201412/365396.html 第二步:在R中输入以下代码: #####SQL SERVER与R语 ...

  5. HDU 5556 最大独立集

    这题主要有了中间的一些连通块的限制,不太好直接用二分图最大独立集做.考虑到图比较小,可以作补图求最大团来求解. #include <iostream> #include <vecto ...

  6. Linux下修改环境终端提示符

    Linux修改环境变量PS1(命令行提示符),可以使用vi编辑/etc/bashrc或/etc/profile文件,在最后加上: export PS1='[\u@\h \W]\$ ' 即可,其中\u显 ...

  7. SQLite数据库_实现简单的增删改查

    1.SQLite是一款轻量型的数据库是遵守ACID(原子性.一致性.隔离性.持久性)的关联式数据库管理系统,多用于嵌入式开发中. 2.Android平台中嵌入了一个关系型数据库SQLite,和其他数据 ...

  8. 谈谈web上种图片应用的优缺点

    web中承载信息的主要方式就是图片与文字了,以下就是对一些web图片格式的优缺点进行归纳. 1.GIF GIF图是比较古老的web图片格式之一,可以追溯到1987,几乎所有的浏览器都支持这一种格式,老 ...

  9. AOJ/初等排序习题集

    ALDS1_1_D-MaximumProfit. Codes: //#define LOCAL #include <cstdio> #include <algorithm> u ...

  10. 线段树区间更新操作及Lazy思想(详解)

    此题题意很好懂:  给你N个数,Q个操作,操作有两种,‘Q a b ’是询问a~b这段数的和,‘C a b c’是把a~b这段数都加上c. 需要用到线段树的,update:成段增减,query:区间求 ...