首页 > 代码库 > leetcode:Maximum Depth of Binary Tree

leetcode:Maximum Depth of Binary Tree

# Definition for a  binary tree node# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass 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