本文主要是介绍Leetcode----104. Maximum Depth of Binary Tree(easy),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:点击打开链接
Python 版一:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution(object):def maxDepth(self, root):""":type root: TreeNode:rtype: int"""if root == None:return 0if root.left == None and root.right != None:return self.maxDepth( root.right ) + 1if root.left != None and root.right == None:return self.maxDepth( root.left ) + 1return max( self.maxDepth( root.left ), self.maxDepth( root.right ) ) + 1
这篇关于Leetcode----104. Maximum Depth of Binary Tree(easy)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!