543. Diameter of Binary Tree
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的更多相关文章
- 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_easy】543. Diameter of Binary Tree
problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
- 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&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 ...
- [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 ...
- 543. Diameter of Binary Tree 二叉树的最大直径
[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...
- 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 ...
- [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 ...
随机推荐
- grid实例(Asp.net)
<link href="../../js/jqGrid/css/ui.jqgrid.css" rel="stylesheet" type="te ...
- socket.io的编程实现
socket.io实例 一.环境要求 客户端需要引用socket.io.js文件 服务端需要按照node环境,以及npm install socket.io用来安装服务端的socket.io 二.客户 ...
- Neural Networks and Deep Learning(神经网络与深度学习) - 学习笔记
catalogue . 引言 . 感知器及激活函数 . 代价函数(loss function) . 用梯度下降法来学习-Learning with gradient descent . 用反向传播调整 ...
- Vmware Vsphere WebService之vijava 开发(二)一性能信息的采集(实时监控)
最近一直没有更新这部分的内容,会利用五一时间完成vcenter这一个系列. 这里先给大家一本关于vijava开发的书,比较实用. 地址:http://pan.baidu.com/s/1gfkl9mj. ...
- Javascript中this关键字
this 是谁调用的时候,指定的是谁,通俗一点讲就是,函数是谁执行是不是由其中一个对象点出来的那就是代表它, 比如执行对象a中b函数a.b();这个b函数中this代表a; 当换成var c=a.b; ...
- 【代码学习】GD库中添加图片水印
函数 getimagesize() bool imagecopymerge( resource dst_im, resource src_im, int dst_x, int dst_y, int s ...
- tomcat的常用配置
1.解決get请求的中文乱码问题 解决办法: 首先找到tomcat路径下的apache-tomcat-7.0.52\conf文件夹,打开server.xml文件,编辑如下内容: <Connect ...
- JavaScript动画1-速度动画
动画实际上就是在一定时间内,改变一个元素的某些属性. 这里简单实现一个JavaScript运动的框架.主要包括: 速度动画(改变left.right.width.height.opacity) 缓冲运 ...
- C#基础知识-流程控制的应用(四)
流程控制我们在编程中运用到的地方非常的多,在上篇中仅仅只是简单的介绍每一种的使用,并没有运用到实例中,很难去理解它真正的作用.下面我们将实际的运用流程控制的代码写一些实例相关的程序,加深对流程控制的理 ...
- hadoop集群中客户端修改、删除文件失败
这是因为hadoop集群在启动时自动进入安全模式 查看安全模式状态:hadoop fs –safemode get 进入安全模式状态:hadoop fs –safemode enter 退出安全模式状 ...