Files
interview/16-LeetCode Hot 100/二叉树的最大深度.md

471 B
Raw Blame History

二叉树的最大深度 (Maximum Depth of Binary Tree)

题目描述

给定一个二叉树,找出其最大深度。

解题思路

DFS / BFS

解法

func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    
    left := maxDepth(root.Left)
    right := maxDepth(root.Right)
    
    if left > right {
        return left + 1
    }
    return right + 1
}

复杂度: O(n) 时间O(h) 空间h 为高度)