Offer.55. I. Er Cha Shu De Shen Du Lcof

剑指 Offer 55 - I. 二叉树的深度 #

Difficulty: 简单

输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。

例如:

给定二叉树 [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

提示:

  1. 节点总数 <= 10000

注意:本题与主站 104 题相同:

题解 #

题解一:DFS(广度优先搜索) #

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }
}

复杂度分析

  • 时间复杂度:

  • 空间复杂度:

Calendar Dec 7, 2020
Edit Edit this page
本站总访问量:  次 您是本站第  位访问者