题目:

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. 【bzoj2561】最小生成树 网络流最小割

    题目描述 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v),那么需要删掉最 ...

  2. 使用ssh建立隧道和web代理

    动态端口转发(socket4/5代理): 通过ssh监听本地端口并把数据转发至远程动态端口 转发local port 至 ssh Server ssh -D ssh -qfTnN -D 本地目标端口 ...

  3. BZOJ-1798 维护序列

    线段树.支持区间加.区间乘.区间查询和. 标记下移还有取模要注意. var n,p,q,i,s,t:longint; a:int64; num,n1,n2,n3:array[0..500000] of ...

  4. 模拟tap事件和longTap事件

    移动端模拟tap和longTap事件,基本原理就是在touchstart和touchend事件中,计算触摸的位移和时间差,位移在一定范围内(轻微滑动),时间小于150ms为tap事件,时间大于300m ...

  5. spring-boot项目MapperScan注解包含多个包

    单个包 @MapperScan("com.mysiteforme.admin.dao") 多个包 @MapperScan({"com.mysiteforme.admin. ...

  6. YY的GCD(bzoj 2820)

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...

  7. C# 打印webBrowser打开的页面

    this.webBrowser.Navigate(webBrowserUrl, tagerFrameName, postBuffer, heads); this.webBrowser.Document ...

  8. Atcoder CODE FESTIVAL 2017 qual B C - 3 Steps 二分图

    题目链接 题意 给定一个无向图,\(n\)个点,\(m\)条边(\(n,m\leq 1e5\)). 重复如下操作: 选择相异的两点u,v满足从点u出发走三条边恰好能到达点v.在这样的u,v点对之间添一 ...

  9. 转 如何在C++中调用C程序

    如何在C++中调用C程序?   C++和C是两种完全不同的编译链接处理方式,如果直接在C++里面调用C函数,会找不到函数体,报链接错误.要解决这个问题,就要在 C++文件里面显示声明一下哪些函数是C写 ...

  10. HDU 3605 Escape 最大流+状压

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 2000/1000 MS (Java/Others)    ...