首页 > 其他 > 详细

leetcode:Maximum Depth of Binary Tree

时间:2014-10-16 01:28:32      阅读:239      评论:0      收藏:0      [点我收藏+]
# 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

原文:http://www.cnblogs.com/CheeseZH/p/4027775.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!