题目:

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. Welcome-to-Swift-09类和结构体(Classes and Structures)

    类和结构体是人们构建代码所用的一种通用且灵活的构造体.为了在类和结构体中实现各种功能,我们必须要严格按照对于常量,变量以及函数所规定的语法规则来定义属性和添加方法. 与其他编程语言所不同的是,Swif ...

  2. 【Luogu】P2221高速公路(线段树乱搞)

    题目链接 这题……我从一开始就想歪了qwq. 为了缅怀逝去的一小时我就把我的30分暴力思路放上来. 首先我们观察枚举的区间.假设我们要枚举的范围是1~5之间的四条路,为了方便我们把它们叫做abcd. ...

  3. 关于cookie使用的一些问题

    保存cookie后提取出来发现字符串是被编码过的,需要decodeURIComponent进行下解码才可以 设置cookie setCookie(c_name, value, expiredays) ...

  4. docker基础——自定义镜像、创建私有仓库、查看 docker 运行状态

    一.自定义镜像 1,案例1 要求:请自定义一个 docker 镜像,基于 hub.c.163.com/library/centos,要求创建出来的镜像在生成容器的时候,可以直接使用 ifconfig ...

  5. luogu 3709 大爷的字符串题 构造 莫队 区间众数

    题目链接 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区间中随机拿出一个字符\(x\),然后把\(x\)从这个区间中删除,你要维护一个集合S 如果\(S\)为空,你\(r ...

  6. 转 Centos下安装apahce的configure: error: APR not found. Please read the documentation解决办法

    转自: http://www.cnblogs.com/Anker/p/3355573.html 今天从Apache官网上http://httpd.apache.org/下载httpd web服务器,由 ...

  7. 第6章 I/O多路复用

    前一章节客户端同时处理两个输入:标准输入和TCP套接字,然而问题在于客户端阻塞于fgets调用期,服务器进程被杀死后,服务器tcp虽然可以正确发送一个fin,但进程正阻塞于标准输入,它无法看到eof, ...

  8. LeetCode OJ--Gray Code **

    http://oj.leetcode.com/problems/gray-code/ 求格雷码的表示,主要应用递归. 递归生成码表 这种方法基于格雷码是反射码的事实,利用递归的如下规则来构造: 1位格 ...

  9. Debian9安装MariaDB

    一:导入密钥并添加了存储库 sudo apt-get install software-properties-common dirmngr sudo apt-key adv --recv-keys - ...

  10. BZOJ 1044 木棍分割(二分答案 + DP优化)

    题目链接  木棍分割 1044: [HAOI2008]木棍分割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3830  Solved: 1453[S ...