leetcode 45:maximum-depth-of-binary-tree

题目描述

求给定二叉树的最大深度,
最大深度是指树的根结点到最远叶子结点的最长路径上结点的数量。

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.


示例1

输入

{1,2}

输出

2
示例2

输入

{1,2,3,4,#,#,5}

输出

3
题目分析:
可以用递归分别计算左子树和右子树的高度,取最大的树高度,代码如下:
1  int maxDepth(TreeNode* root) {
2         if(root == NULL)
3             return 0;
4         return max(maxDepth(root->left),maxDepth(root->right)) + 1;
5     }

 

posted @ 2020-08-07 09:42  请叫我小小兽  阅读(122)  评论(0编辑  收藏  举报