其他分享
首页 > 其他分享> > LeetCode104-二叉树的最大深度

LeetCode104-二叉树的最大深度

作者:互联网

原题链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

解题思路:递归

代码:

 1 # Definition for a binary tree node.
 2 # class TreeNode:
 3 #     def __init__(self, val=0, left=None, right=None):
 4 #         self.val = val
 5 #         self.left = left
 6 #         self.right = right
 7 class Solution:
 8     def maxDepth(self, root: Optional[TreeNode]) -> int:
 9         if not root:
10             return 0
11         return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

 

标签:right,val,self,LeetCode104,二叉树,深度,maxDepth,root,left
来源: https://www.cnblogs.com/BigPeng965/p/15790490.html