题目:

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的更多相关文章

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

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

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

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

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

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

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

  8. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

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

随机推荐

  1. 刷题总结——Cut the Sequence(POJ 3017 dp+单调队列+set)

    题目: Description Given an integer sequence { an } of length N, you are to cut the sequence into sever ...

  2. 浅谈Android反调试 之 PTRACE_TRACEME

    反调试原理: 关于Ptrace:  http://www.cnblogs.com/tangr206/articles/3094358.html ptrace函数 原型为:     #include & ...

  3. jenkins配置本机JDK和maven环境

    1.jenkins官网下下载jenkins的war包 2.安装jenkins,启动命令:java  -jar jenkins.war 3.打开http://localhost:8080/ 4.点击系统 ...

  4. 【前端学习笔记】2015-09-06 ~~~~ setAttribute()、slice()

    所遇记录: 1.setAttribute("属性",value),相同的还有addAttribute("属性名",value),getAttribute(“属性 ...

  5. 【NOIP2016练习】T3 质数 (分块)

    题意:共有N盏灯,标号为1到N,有M个标有不同质数的开关,开关可以控制所有标号为其标号倍数的灯,按一次开关,所有其控制的灭着的灯都点亮,所有其控制的亮着的灯将熄灭.现在,宿管可以无限的按所有开关,所有 ...

  6. 标准C程序设计七---110

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  7. sqlite 使用

    '''SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说 没有独立的维护进程,所有的维护都来自于程序本身. 在python中,使用sqlite3创建数据库的连接,当我们指定的数据库文件不 ...

  8. java三种匿名的方式开启线程

    package demo04; /* * 使用匿名内部类,实现多线程程序 * 前提:继承或者接口实现 * new 父类或者接口(){ * 重写 抽象方法 * } */ public class Thr ...

  9. SQL Server 内置函数实现MD5加密

    一.MD5加密 HASHBYTES ('加密方式', '待加密的值')     加密方式= MD2 | MD4 | MD5 | SHA | SHA1     返回值类型:varbinary(maxim ...

  10. IOS-<input>表单元素只能读,设置readonly时光标仍然可见的解决办

    在HTML中,如果把一个<input>的readonly属性设置为"readonly",表示这个表单元素不能编辑. 但是,鼠标点击之后,这个表单元素还是有光标存在的. ...