leetcode:Maximum Depth of Binary Tree【Python版】
# 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
l,r = 1,1
if root.left != None:
l = l+self.maxDepth(root.left)
if root.right != None:
r = r+self.maxDepth(root.right)
if l>r:
return l
else:
return r
1、在开始的时候经常遇到“NoneType”Error,后来查阅资料才知道使用 root==None 就可以处理这种情况;
2、调用函数的时候不需要传递self值,这个应该是Python自己传递的,如果自己添加上,反而会报错;
3、这道题目说明不是很清晰,系统的输入是{},{0},{0,0,0,0,#,#,0,#,#,#,0},{1,2}等形式,然后后台会自动构建二叉树;
leetcode:Maximum Depth of Binary Tree【Python版】的更多相关文章
- leetcode Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [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 ...
- Leetcode 104 Maximum Depth of Binary Tree python
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 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 ...
- [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 ...
- [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
- leetcode Minimum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
随机推荐
- 使用dbms_output.put_line打印异常所在的行
dbms_output.put_line(dbms_utility.format_error_stack); dbms_output.put_line(dbms_utility.format_call ...
- silent install oracle 11.2.0.1 x86_64 for linux
su - root#groupadd oinstall#useradd -g oinstall oracle#passwd oracle#mkdir -p /u01/app/oracle#chown ...
- Python将列表作为栈和队列
Collections中的各种方法 阅读目录(Content) 一.各种方法介绍 二.代码部分 回到顶部(go to top) 一.各种方法介绍 Counter 统计个数 elements mo ...
- Adaboost入门教程——最通俗易懂的原理介绍(图文实例)
https://blog.csdn.net/px_528/article/details/72963977 写在前面 说到Adaboost,公式与代码网上到处都有,<统计学习方法>里面有详 ...
- POJ 2485 最小生成树
2333333333 又是水题.prim模板直接水过.求最小生成树里的最大的边的权值. 附代码:// 如果我木猜错的话.是要求最小生成树的最大边值. #include<stdio.h>#i ...
- WEBSERVICE-AXIS2服务端代码
下载axis2的插件 axis2-eclipse-codegen-plugin-1.7.1.zip axis2-eclipse-service-plugin-1.7.1.zip 解压后,将plugin ...
- python3.6 连接mysql数据库
==================pymysql=================== 由于 MySQLdb 模块还不支持 Python3.x,所以 Python3.x 如果想连接MySQL需要安装 ...
- Jena RDF API
1. jena 简单使用 RDF可以用简单的图示:包括节点以及连接节点的带有箭头的线段来理解. 这个例子中,资源 http://.../JohnSmith 表示一个人.这个人的全名是 John Sm ...
- DevExpress v17.2新版亮点——CodeRush篇(三)
用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了CodeRush v17.2 的新功能,快来下载试用新版本! 代码格式和清理 文档 ...
- bean的实例化
bean的实例化 构造器方式 静态方法方式 普通工厂方式 一般的,默认bean实例化使用的是构造器方式,调用的是无参的构造方法 package com.Model; public class Dog ...