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

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

标签:剑指 Offer发布于:编辑于:浏览量:1539

概述

https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/

https://leetcode.com/problems/maximum-depth-of-binary-tree/

递归法

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (!root) return 0;
        return 1 + max(maxDepth(root->left), maxDepth(root->right));
    }
};