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 = ...
随机推荐
- mysql数据库切分
一.数据的垂直切分概念:数据的垂直切分,也可以称之为纵向切分.将不同的表分散到不同的数据库主机中.一个应用系统,总体功能肯定是由很多个功能模块所组成的,而每一个功能模块所需要的数据对应到数据库中就是一 ...
- LeetCode--121--卖卖股票的最佳时机
问题描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出 ...
- 3-13《元编程》第5章Class Definitions 3-14(5-4Singleton Classes,2小时)3-15(3小时✅)
类宏 环绕别名 singleton class Class就是增强的模块,类定义也就是模块定义. 5.1 Class Definitions Demystified 5.11 Inside Class ...
- Confluence 6 LDAP 高级设置
启用嵌套组(Enable Nested Groups) 为嵌套组启用或禁用支持. 一些目录服务器能够允许你在一个组中定义另外一个组.在这种结构下的用户组称为用户组嵌套.嵌套组的配置能够让子用户组继承上 ...
- 百度定位SDK
按照官网要求配置SHA1和包名生成ak秘钥 生成秘钥命令: keytool -list -v -keystore debug.keystore 密码:原始密码为android 添加libs文件夹并在g ...
- Leetcode 22
//这题感觉不如前两题回溯清楚,还要再看看class Solution { public: vector<string> generateParenthesis(int n) { vect ...
- iOS UI-UIScrollView控件实现图片轮播 (UIPageControl-分页指示器)
一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "ViewController.h" #define HM ...
- Word发布到cnblogs文章
1◆ 打开word 2◆ 注册用户 3◆ 操作 success
- Openwrt Support RESET Button (5)
1 Scope of Document This document describes how to support reset button under openwrt system2 Requir ...
- Python Threading多线程简单例子
业务监控,多线程例子,实现每类个监控项的不同监控间隔. #coding=utf-8import sysimport pymysqlimport osfrom prometheus_client imp ...