• 力扣刷题-二叉树-二叉树的高度与深度


    二叉树最大深度

    给定一个二叉树 root ,返回其最大深度。
    二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
    示例 1:
    image.png
    输入:root = [3,9,20,null,null,15,7]
    输出:3

    递归法

    本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。
    二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
    二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
    根节点的高度就是二叉树的最大深度
    ,所以本题中我们通过
    后序
    求的根节点高度来求的二叉树最大深度。
    这一点其实是很多同学没有想清楚的,很多题解同样没有讲清楚。
    image.png
    体现后序遍历的过程!!!使用前序的话要复杂的多
    递归第一点:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
    递归第二点:如果为空节点的话,就返回0,表示高度为0。
    递归第三点:
    **先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 **(加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。(也就是高度)

    class solution:
        def maxdepth(self, root: treenode) -> int:
            return self.getdepth(root)
        def getdepth(self, node):
            if not node:
                return 0
            leftdepth = self.getdepth(node.left) # 左
            rightdepth = self.getdepth(node.right) # 右
            depth = max(leftdepth, rightdepth) + 1 # 中
            return depth
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    精简版:

    class solution:
        def maxdepth(self, root: treenode) -> int:
            if not root:
                return 0
            return 1 + max(self.maxdepth(root.left), self.maxdepth(root.right))
    
    • 1
    • 2
    • 3
    • 4
    • 5

    层次遍历

    from collections import deque
    
    class TreeNode(object):
        def __init__(self, val=0, left=None, right=None):
            self.val = val
            self.left = left
            self.right = right
    
    class Solution(object):
        def maxDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if not root:
                return 0
            result = []
            queue = deque([root])
            while queue:
                level_result = []
                for _ in range(len(queue)):
                    cur = queue.popleft()
                    level_result.append(cur.val)
                    if cur.left:
                        queue.append(cur.left)
                    if cur.right:
                        queue.append(cur.right)
                result.append(level_result)
            return len(result) # 里面多少嵌套列表 即为最大深度
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    参考:
    https://www.programmercarl.com/0104.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%A4%A7%E6%B7%B1%E5%BA%A6.html

  • 相关阅读:
    Vue2(一):Vue介绍、模板语法、数据绑定、el和data的两种写法、MVVM、数据代理、事件
    leetcode 93: 复原IP地址 (面试常考)
    Python(八十八)函数的参数传递
    【CLI命令行接口和Java连接openLooKeng查询数据 】
    【python】python内置函数 ——round()获取浮点数的四舍五入值
    Taurus.MVC WebMVC 入门开发教程2:一个简单的页面呈现
    Pycharm----将Anaconda建立的环境导入
    Ubuntu-虚拟机常见问题
    Java中线程安全的集合
    redis的常用基础类型及操作
  • 原文地址:https://blog.csdn.net/hxhabcd123/article/details/134520169