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 ...
随机推荐
- 【PAT_Basic日记】1005. 继续(3n+1)猜想
#include <stdio.h> #include <stdlib.h> /** 逻辑上的清晰和代码上的清晰要合二为一 (1)首先在逻辑上一定要清晰每一步需要干什么, (2 ...
- angular ng-bind
<body ng-app=""> <div ng-controller="firstController"> <input typ ...
- C++queue容器学习(详解)
一.queue模版类的定义在<queue>头文件中. queue与stack模版非常类似,queue模版也需要定义两个模版参数,一个是元素类型,一个是容器类型,元素类型是必要的,容器类型是 ...
- nginx源码分析——http模块
源码:nginx 1.12.0 一.nginx http模块简介 由于nginx的性能优势,现在已经有越来越多的单位.个人采用nginx或者openresty. ...
- LESS的一点自己的理解(2)
上次写的一点居然忘了保存了,虽然说编辑器有自动保存的功能,但是昨天写的依然找不到了,/(ㄒoㄒ)/~~那好吧,重新开始写. 1.上篇写到了Mixins(混入),如果你仔细看了上面的例子,你就会发现其实 ...
- 对象克隆(clone)实例详解
<?php class Staff { public $name; public $age; public $salary; public function __construct($name, ...
- react native 升级到0.31.0的相关问题 mac xcode开发环境
cmd + D和cmd + R快捷键没有反应 0.31.0版本换了一种加载方式,通过修改userDefaults达到debug目的 [userDefaults setObject:@"127 ...
- python中的一些小知识
在最近学习python中遇到的一些小问题汇总一下: 1.在windows7下安装python3.5版本时提示安装不了,缺少ServicePack1. 解决办法是,打开控制面板\系统和安全\Windo ...
- OC—Setter、Getter
一.本篇以Setter和Getter 来进行成员变量的赋值. 二.Setter 与 Getter 1. 命名规范 为对象中的某个实例变量赋值的方法称为修改方法,用来修改对象的状态这类修改方法称为set ...
- [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile
题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...