104. 二叉树的最大深度【二叉树】

104. 二叉树的最大深度

104. 二叉树的最大深度

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

示例 1:

在这里插入图片描述

输入:root = [3,9,20,null,null,15,7]
输出:3

示例 2:

输入:root = [1,null,2]
输出:2

提示:

  • 树中节点的数量在 [0, 10^4] 区间内。
  • -100 <= Node.val <= 100

解法一:层序遍历

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    // 层序遍历,层数就是最大深度
    queue := make([]*TreeNode,0)
    queue = append(queue,root)
    res := 0
    for len(queue) > 0 {
        queueSize := len(queue)
        for i := 0;i < queueSize;i++ {
            curNode := queue[0]
            queue = queue[1:]   
            if curNode.Left != nil {
                queue = append(queue,curNode.Left)
            }
            if curNode.Right != nil {
                queue = append(queue,curNode.Right)
            }
        }
        // 遍历完一层后,层数+1
        res += 1  
    }

    return res
}

解法二:深度递归法

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    // 使用前序遍历,不断更新最大深度的变量值
    res := 1
    dfs(root,1,&res)
    return res
}

func dfs(root *TreeNode,depth int,res *int)  {
    if *res < depth { // 中
        *res = depth
    }

    if root.Left != nil { // 左
        depth += 1 // 深度+1
        dfs(root.Left,depth,res)
        depth -= 1 // 回溯,深度-1
    }

    if root.Right != nil { // 右
        depth += 1 // 深度+1
        dfs(root.Right,depth,res)
        depth -= 1 // 回溯,深度-1
    }

}

解法三:粗暴递归法

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    // 这里实际是后续遍历(左右根),不过用一行写了,实际可以改成如下四行
    // leftDepth := maxDepth(root.Left) // 左
    // rightDepth := maxDepth(root.Right) // 右
    // 根(遍历到根时就是具体的处理逻辑,遍历到左右节点时则是递归)
    // curNodeDepth = max(leftDepth,rightDepth) + 1 
    // return curNodeDepth
    return max(maxDepth(root.Left),maxDepth(root.Right)) + 1
}

func max(a,b int) int {
    if a > b {
        return a
    }

    return b
}

在这里插入图片描述

计算二叉树最大深度算法中一个常见的问题,通常可以通过递归或者迭代的方式实现。下面将分别介绍这两种方法的具体实现。 ### 1. 使用递归的方法 递归是一种直观且简单的方法,其核心思想是:当前节点的深度等于左子树和右子树深度的最大值加上1(当前节点本身的深度)。递归的终止条件是当节点为空时,返回深度0。 以下是基于递归方法的Java代码实现: ```java class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } } public class BinaryTreeMaxDepth { public int maxDepth(TreeNode root) { if (root == null) { return 0; } int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right); return Math.max(leftDepth, rightDepth) + 1; } public static void main(String[] args) { // 构建二叉树 TreeNode root = new TreeNode(3); root.left = new TreeNode(9); root.right = new TreeNode(20); root.right.left = new TreeNode(15); root.right.right = new TreeNode(7); BinaryTreeMaxDepth solution = new BinaryTreeMaxDepth(); int maxDepth = solution.maxDepth(root); System.out.println("二叉树最大深度为:" + maxDepth); } } ``` ### 2. 使用迭代的方法 与递归方法不同,迭代方法使用队列来模拟递归的过程,通过层级遍历的方式计算最大深度。这种方法可以避免递归可能导致的栈溢出问题。 以下是基于迭代方法的Java代码实现: ```java import java.util.LinkedList; import java.util.Queue; class Solution { public int maxDepth(TreeNode root) { if (root == null) { return 0; } Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); int level = 0; while (!queue.isEmpty()) { int count = queue.size(); level++; while (count > 0) { TreeNode current = queue.poll(); if (current.left != null) { queue.offer(current.left); } if (current.right != null) { queue.offer(current.right); } count--; } } return level; } } ``` ### 方法对比 - **递归方法**:实现简单、代码简洁,但可能在大规模数据的情况下导致栈溢出。 - **迭代方法**:使用队列进行层级遍历,避免了栈溢出的问题,适用于更广泛的数据规模[^1]。 两种方法都能正确计算二叉树最大深度,选择哪一种取决于具体的应用场景和个人偏好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值