《剑指Offer》刷题笔记——面试题55-I. 二叉树的深度

难度:简单

一、题目描述:

在这里插入图片描述

二、解题分析:

1、剑指解析

在这里插入图片描述
在这里插入图片描述

2、代码实现

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root is None: 
            return 0 
        else: 
            left_height = self.maxDepth(root.left) 
            right_height = self.maxDepth(root.right) 
            return max(left_height, right_height) + 1 
发布了132 篇原创文章 · 获赞 154 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_34108714/article/details/104756621