leetcode:Maximum Depth of Binary Tree【Python版】

时间:2022-08-31 18:43:39
# 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版】的更多相关文章

  1. leetcode Maximum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  2. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

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

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

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

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

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

  8. LeetCode Maximum Depth of Binary Tree (求树的深度)

    题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...

  9. leetcode Minimum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

随机推荐

  1. Android基础学习第二篇—Activity

    写在前面的话: 1. 最近在自学Android,也是边看书边写一些Demo,由于知识点越来越多,脑子越来越记不清楚,所以打算写成读书笔记,供以后查看,也算是把自己学到所理解的东西写出来,献丑,如有不对 ...

  2. log4net.redis+logstash+kibana+elasticsearch+redis 实现日志系统

    前端时间写了个随笔 log4net.NoSql +ElasticSearch 实现日志记录 ,因项目原因需要把日志根java平台的同事集成采用logstash+kibana+elasticsearch ...

  3. 百度编辑器ueditor代码高亮效果前台不显示的解决方法

    原因是你没有在你的内容页加载相应的css文件,这要如何解决呢? 经测试,只要插入以下两个文件即可解决问题: <link href="你的ueditor路径/ueditor/third- ...

  4. Mac中的快捷键

    Mac中主要有四个修饰键,分别是Command,Control,Option和Shift,这四个键分别有自己的图案 基本快捷键 Command是Mac里最重要的修饰键,在大多数情况下相当于Window ...

  5. python2&period;6升级到2&period;7

    开发部需要使用python2.7,由于公网的环境python版本都是系统自带的,版本是2.6,需要升级,最好是通过RPM升级. Linux系统:CentOS CentOS 6.4升级Python后yu ...

  6. 二维码的妙用:通过Zxing实现wifi账号password分享功能

    二维码是搭载信息的一种载体,通过二维码能够传递名片.网址.商品信息等,本文讲到二维码的第二种妙用:通过二维码实现wifi账号和password分享. 关于二维码的基础知识,请訪问:二维码的生成细节和原 ...

  7. Batch File Rename Utility&lpar;文件批量改名软件&rpar; 1&period;1&period;4231

    软件名称: Batch File Rename Utility(文件批量改名软件) 1.1.4231.23098 软件语言: 英文 授权方式: 免费软件 运行环境: Win7 / Vista / Wi ...

  8. Django学习-7-ORM操作

    select * from tb where id > 1    # 对应关系     models.tb.objects.filter(1id__gt=)         models.tb. ...

  9. react生命周期函数

      如图,可以把组件生命周期大致分为三个阶段: 第一阶段:是组件第一次绘制阶段,如图中的上面虚线框内,在这里完成了组件的加载和初始化: 第二阶段:是组件在运行和交互阶段,如图中左下角虚线框,这个阶段组 ...

  10. Ubuntu相关配置

    1.Ubuntu配置root密码 2.开机ssh连接--nat   编辑网络设置端口转发