【EASY】Maximum Depth of Binary Tree

发布于:2019-03-05 23:33,阅读数:483,点赞数:0


# 问题 原题链接:[https://leetcode.com/problems/maximum-depth-of-binary-tree/](https://leetcode.com/problems/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. Note: - A leaf is a node with no children. Example: ``` Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its depth = 3. ``` # 分析过程 - 输入:一棵二叉树 - 输出:二叉树的最大深度 - 思路:最大深度 = max(左子树最大深度, 右子树最大深度) # 解决方法 ```cpp class Solution { public: int maxDepth(TreeNode* root) { if (!root) { return 0; } return 1 + max(maxDepth(root->left), maxDepth(root->right)); } }; ```


评论:0条


返回列表

返回主页