LeetCode-104. Maximum Depth of Binary Tree
来源:互联网 发布:java如何记录接口日志 编辑:程序博客网 时间:2024/05/24 05:27
问题:https://leetcode.com/problems/maximum-depth-of-binary-tree/?tab=Description
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。用递归比较容易求得。
参考C++代码:
/** * 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(NULL==root) return 0; if(NULL==root->left && NULL==root->right) return 1; int l=maxDepth(root->left); int r=maxDepth(root->right); return l>=r?l+1:r+1; }};
0 0
- [LeetCode]104.Maximum Depth of Binary Tree
- 【LeetCode】104.Maximum Depth of Binary Tree
- [Leetcode] 104. Maximum Depth of Binary Tree
- LeetCode --- 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- leetCode 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree LeetCode
- 104. Maximum Depth of Binary Tree LeetCode
- LeetCode 104. Maximum Depth of Binary Tree
- leetcode:104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- [LeetCode]104. Maximum Depth of Binary Tree
- leetcode 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree
- LeetCode#104. Maximum Depth of Binary Tree
- Leetcode 104. Maximum Depth of Binary Tree
- LeetCode *** 104. Maximum Depth of Binary Tree
- 跑马灯实现的三种方式
- 数据库事务的四大特性以及事务的隔离级别
- Git常用命令总结
- redis持久化,主从及数据备份
- JDBC中的Statement和PreparedStatement的区别
- LeetCode-104. Maximum Depth of Binary Tree
- Java 计算两个日期相差年数字、月数、天数及时分秒
- 开通博客啦!我的第一个博客
- poj 1753 Flip Game (状压DP)@
- 图说设计模式
- Android 内存泄漏的简单检查与分析方法
- VR几年内会发展成什么样
- drools6 KSession 为空的可能解决方法
- 基于 Jenkins 快速搭建持续集成环境