题目:

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.

求二叉树的深度,递归一下就可以求到了。

python新手(我)发现python中没有三元运算符,不过可以用true_part if condition else false_part模拟。虽然我后面用max函数代替了三元运算符,也实现了功能。

 class Solution(object):
def maxDepth(self, root):
if (root == None):
return 0
left_depth = self.maxDepth(root.left)
right_depth = self.maxDepth(root.right)
return max(left_depth, right_depth) + 1

Leetcode 104 Maximum Depth of Binary Tree python的更多相关文章

  1. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  2. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  3. [LeetCode] 104. 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. (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. LeetCode 104 Maximum Depth of Binary Tree 解题报告

    题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  7. LeetCode 104. Maximum Depth of Binary Tree

    Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...

  8. leetcode 104 Maximum Depth of Binary Tree ----- java

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. Java [Leetcode 104]Maximum Depth of Binary Tree

    题目描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...

随机推荐

  1. a foreign key constraint fails

    今天遇到一个问题,一对一级联保存时,报错如下: Cannot add or update a child row: a foreign key constraint fails 解决方法: MySQL ...

  2. tomcat server.xml 配置示例

    规划:     网站网页目录:/web/www      域名:www.test1.com     论坛网页目录:/web/bbs     URL:bbs.test1.com/bbs     网站管理 ...

  3. C语言解析日志,存储数据到伯克利DB

    编译命令 gcc -o dbwriter dbwriter.c -ldb dbwriter.c #include <assert.h> #include <stdlib.h> ...

  4. cf C. Magic Formulas

    http://codeforces.com/contest/424/problem/C #include <cstdio> #include <cstring> #includ ...

  5. poj2484--A Funny♂Game

    题意:n个硬币围成环,每次可以取相邻两个硬币或者一个,不能取者负. Ans:n<=2的时候先手必胜,其他任意情况后手必胜,因为不论我先手取什么,我后手总有中心对称与它配对. #include&l ...

  6. 《Programming WPF》翻译 第6章 5.我们进行到哪里了?

    原文:<Programming WPF>翻译 第6章 5.我们进行到哪里了? WPF提供了资源工具,让我们运用在用户界面中,动态并具有一致性.我们可以在资源字典中存储任意资源,并且可以遍及 ...

  7. 【转】linux tree命令以树形结构显示文件目录结构 ---- 不错

    原文网址:http://jingyan.baidu.com/article/acf728fd19c7eff8e510a3eb.html 今天小编来给分享Linux 系统下一个非常有用的命令的使用:tr ...

  8. 异步套接字基础:select函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET

    参考:[原创]技术系列之 网络模型(三)多路复用模型 select函数 select函数: 系统提供select函数来实现多路复用输入/输出模型.原型: #include <sys/time.h ...

  9. poj1330Nearest Common Ancestors(LCA小结)

    题目请戳这里 题目大意:意如其名. 题目分析:本题只有一个查询,所以可以各种乱搞过去. 不过对于菜鸟而言,还是老老实实练习一下LCA算法. LCA有很多经典的算法.按工作方式分在线和离线2种. tar ...

  10. Repeater里面加上if判断

    //创建时绑定 protected void ItemCreated(object sender, RepeaterItemEventArgs e) { if (e.Item.DataItem != ...