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

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. 

class Solution:
def TreeDepth(self, node):
if node == None:
return 0
left_depth = Solution.TreeDepth(self, node.left)
right_depth = Solution.TreeDepth(self, node.right)
return max(left_depth, right_depth) + 1

Note:

1 It is implemented in a recursive manner. 

Sol:

# 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.max_len = 0
def depth(node):
if not node:
return 0
left_depth = depth(node.left)
right_depth = depth(node.right)
self.max_len = max(self.max_len, left_depth + right_depth)
return max(left_depth, right_depth) + 1
depth(root)
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. 第六章 Hibernate关联映射

    第六章 hibernate关联映射一.本章知识点分为2部分:1.关联关系:单向多对一关联关系,双向一对多关联关系(含一对多关联关系),多对多关联关系2.延迟加载:类级别加载策略,一对多加载策略,多对一 ...

  2. SSH整合总结(xml与注解)

    本人自己进行的SSH整合,中间遇到不少问题,特此做些总结,仅供参考. 一.使用XML配置: SSH版本 Struts-2.3.31 Spring-4.3.5 Hibernate-4.2.21 引入ja ...

  3. three.js提供的几何体

    1.简单几何体 three.js提供的稍微简单点的几何体包括有:PlaneGeometry(平面).CircleGeometry(圆形).ShapeGeometry(塑性).CubeGeometry( ...

  4. Linux防火墙配置—允许转发

    一.实验目标 在上一次"Linux基础网络搭建实验"中,内.外网虚拟机之所以能Ping通,是因为暂时关闭了防火墙,然而现实中这样操作显然存在很大的安全隐患,所以本次实验在上次实验的 ...

  5. 兼容IE8的input输入框的正确使用姿势

    input是一个很常见的标签,大家使用的也很常见,但是我在具体的工作中发现要想完美的使用这个标签还是任重而道远,下面是我碰到的几个问题. 1.我们在使用这个标签的时候会习惯的加上placeholder ...

  6. Struts2框架的基本使用

         前面已经介绍过了MVC思想,Struts2是一个优秀的MVC框架,大大降低了各个层之间的耦合度,具有很好的扩展性.从本篇开始我们学习Struts2的基本用法,本篇主要包括以下内容: Stru ...

  7. 使用虚拟机CentOS7部署CEPH集群

    第1章   CEPH部署 1.1  简单介绍 Ceph的部署模式下主要包含以下几个类型的节点 Ø CephOSDs: A Ceph OSD 进程主要用来存储数据,处理数据的replication,恢复 ...

  8. 【算法系列学习】codeforces C. Mike and gcd problem

    C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...

  9. 利用GDB进行多线程调试

    一.多线程调试 多线程调试重要就是下面几个命令: info thread 查看当前进程的线程. thread <ID> 切换调试的线程为指定ID的线程. break file.c:100 ...

  10. 分针网—IT教育:作为PHP开发人员容易忽视的几个重点

    无论是学习什么样的一个开发.ASP开发.java开发.当学习还不是很久的时候,一般都是不知道它们的精华是在哪里,而现在很多的php程序员也是不知道PHP的精华所在,为什么perl在当年在商界如此的出名 ...