欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

【leetcode】104. Maximum Depth of Binary Tree

发布时间:2023/12/19 53 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【leetcode】104. Maximum Depth of Binary Tree 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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. 思路

递归到左、右子树的max值,+1.

3. 代码

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

总结

以上是生活随笔为你收集整理的【leetcode】104. Maximum Depth of Binary Tree的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。