[LeetCode]二叉树的最大深度

本文介绍了一种使用递归方法求解二叉树最大深度的算法,并提供了一个优化后的高效实现方案。

///////////////////////////////////////////////////////////////////////////////////////////////////////

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

///////////////////////////////////////////////////////////////////////////////////////////////////////

树的遍历还是用递归比较好,虽然很长时间不碰代码,但还是硬着头皮写了。

思路很简单,每遍历一个结点,层数计数+1,直到叶子节点,把得到的层数与最大层数比较大小,最终返回最大层数。

果然,还是写的太笨了,放在这仅供参考。

class Solution 
{
	int mdepth = 0;
public:
	int isEnd(int d, TreeNode *r)
	{
		d++;
		TreeNode *left = r->left;
		TreeNode *right = r->right;
		if (left == NULL)
		{
			if (d > mdepth)
				mdepth = d;
		}
		else
			isEnd(d, left);
		if (right == NULL)
		{
			if (d > mdepth)
				mdepth = d;
		}
		else
			isEnd(d, right);
		return mdepth;
	}

	int maxDepth(TreeNode* root) 
	{
		int depth = 0;
		if (root == NULL)
			return mdepth;
		mdepth = isEnd(depth, root);
		return mdepth;
	}
};

顺便贴一个4ms的范例:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int max = 0;
        if (root != NULL) {
            max++;
            int max_left = maxDepth(root->left);
            int max_right = maxDepth(root->right);

            max += max_left>max_right ? max_left:max_right;
        }
        return max;
    }
};

差距可以说是很明显了。

在 C++ 的 Boost 库中,`boost::posix_time` 和 `boost::gregorian` 是两个不同的命名空间,分别用于处理时间与日期相关的类型和函数。它们的使用场景如下: ### `boost::posix_time` 命名空间 该命名空间主要用于处理时间点(`ptime`)和时间间隔(`time_duration`),也包含与完整时间戳相关的一些功能。以下类型的定义和函数需要加上 `boost::posix_time` 前缀: - **时间点类型**:`boost::posix_time::ptime` 用于表示一个具体的时间点,例如当前时间或某个特定时刻。 - **时间间隔类型**:`boost::posix_time::time_duration` 表示一段时间,例如小时、分钟和秒的差值。 - **时间周期类型**:`boost::posix_time::time_period` 用于描述一个时间段范围,可以判断某时间点是否在指定范围内[^2]。 - **获取当前时间函数**:例如 `boost::posix_time::second_clock::local_time()` 或 `boost::posix_time::microsec_clock::universal_time()`,用来获取系统当前的时间点。 - **转换函数**:如 `boost::posix_time::to_tm()`,用于将 `ptime` 转换为标准库中的 `tm` 结构[^3]。 ### `boost::gregorian` 命名空间 这个命名空间专注于日期操作,涵盖了日期对象及其相关计算。以下类型和函数需要加上 `boost::gregorian` 前缀: - **日期类型**:`boost::gregorian::date` 表示一个具体的日期,支持创建、比较以及加减天数等操作。 - **日期间隔类型**:`boost::gregorian::date_duration` 描述日期之间的差值,以天为单位。 - **日期周期类型**:`boost::gregorian::date_period` 用于表示一段日期范围,可以通过 `contains()` 方法判断某个日期是否处于周期内[^2]。 - **日期生成函数**:包括 `boost::gregorian::from_string()` 和 `boost::gregorian::from_undelimited_string()` 等,用于从字符串构造日期对象[^5]。 - **特殊日期枚举**:例如 `boost::date_time::min_date_time` 或 `boost::date_time::not_a_date_time`,用于创建具有特殊含义的日期对象。 - **日期访问函数**:例如 `is_not_a_date()` 判断日期是否无效,或者 `day()` 获取日期中的日部分。 ### 使用方式 在实际代码中,为了简化书写,通常会通过 `using namespace boost::posix_time;` 或 `using namespace boost::gregorian;` 引入整个命名空间。也可以显式地对每个类型或函数添加前缀,避免潜在的命名冲突。例如: ```cpp #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/gregorian/gregorian.hpp> int main() { // 使用 boost::posix_time 处理时间 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); // 使用 boost::gregorian 处理日期 boost::gregorian::date today = boost::gregorian::day_clock::local_day(); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值