Leetcode_num2_Maximum Depth of Binary Tree
题目:
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
AC率第二高的题啦。二项树的最长路径。初看此题就感觉要用递归,但不知怎的。一開始想到深度遍历上去了。。。。囧
实际上非常easy的,某一节点的最长路径=max(该节点左子树的最长路径,该节点右子树的最长路径)+1
另一点就是类中函数调用函数时格式为 self.函数名,否则会报 global name XXX is not defined
废话不多说啦,上代码咯
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def maxDepth(self, root):
if root==None:
return 0
else:
p=max(self.maxDepth(root.left),self.maxDepth(root.right))+1
return p
补充更新ing~~~~
今天刷笔试题的时候又遇到了这道题,可是仅仅能用c++来写,于是高速地写出了例如以下代码:
class Solution {
public:
int maxDepth(TreeNode *root) {
if (root==NULL)
return 0;
else{
int rs=0;
if(maxDepth(root->left)>maxDepth(root->right)){
rs=1+maxDepth(root->left);
}
else{
rs=1+maxDepth(root->right);
}
return rs;
}
}
};
一执行,结果TLE了。
。。。。
囧
细致检查发现该程序在推断和计算的过程中反复调用了递归函数,添加了算法复杂度,因此会出现TLE
改动后的代码例如以下:
class Solution {
public:
int maxDepth(TreeNode *root) {
if (root==NULL)
return 0;
else{
int rs=0;
int left=maxDepth(root->left);
int right=maxDepth(root->right);
if(left>right){
rs=1+left;
}
else{
rs=1+right;
}
return rs;
}
}
};
Leetcode_num2_Maximum Depth of Binary Tree的更多相关文章
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
随机推荐
- iOS-----openGL--openGL ES iOS 入门篇4---> 离屏渲染
http://www.cnblogs.com/CoderAlex/p/6604618.html 通常情况下,我们使用openGL将渲染好的图片绘制到屏幕上,但有时候我们不想显示处理结果,这时候就需要使 ...
- C# Settings.settings的用处
1.定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: 2.读取配置值 t ...
- Codeforces 898E Squares and not squares
题目大意 给定 $n$($n$ 是偶数,$2\le n\le 2\times 10^{5}$)个非负整数 $a_1,\dots, a_n$($a_i\le 10^9$). 要求将其中 $n/2$ 个数 ...
- java面试题之如何判断一个对象是否应该被回收
常用的有两种办法: 引用计数法:(无法解决对象循环引用的问题,导致对象无法被回收) 可达性分析:
- 浅谈Linux下的五种I/O模型 两篇别人的博客
http://blog.csdn.net/sinat_34990639/article/details/52778562 http://www.cnblogs.com/chy2055/p/5220 ...
- La 4670 AC自动机(模版)
#include<iostream> #include<cstring> #include<queue> #include<cstdio> #inclu ...
- 标准C程序设计七---64
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- HDU 4405: Aeroplane chess
类型:概率DP 题意:一条直线下飞行棋,色子六个面等概率.同时存在一些飞机航线,到了某个点可以直接飞到后面的另一个点,可以连飞,保证一个点至多一条航线.求到达或者超过终点 所需要 掷色子的期望次数. ...
- Unsafe in Java
http://www.cnblogs.com/xrq730/p/4976007.html http://www.importnew.com/14511.html http://blog.csdn.ne ...
- BZOJ——2438: [中山市选2011]杀人游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=2438 Description 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个 ...