[leetcode]: 104. Maximum Depth of Binary Tree

系统管理员 2022-06-16 13:08 310阅读 0赞

1.题目描述

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
求一个二叉树的最大深度

2.分析

可以深度优先搜索或广度优先搜索。

3.代码

深搜,递归,c++

  1. int maxDepth(TreeNode* root) {
  2. if (root == NULL)
  3. return 0;
  4. else
  5. return 1 + max(maxDepth(root->left), maxDepth(root->right));
  6. }

发表评论

表情:
评论列表 (有 0 条评论,310人围观)

还没有评论,来说两句吧...

相关阅读