leetcode 104、559学习笔记(求树的深度,迭代递归)

leetcode 104 二叉树的最大深度

传送门
在这里插入图片描述

思路

1、递归法

① 确定递归函数的参数和返回值:
参数就是 传入树的根节点,返回值就是返回这棵树的深度,所以 返回值为int类型。

	int getDepth(TreeNode* node)

②确定终止条件:
如果为空节点的话,就返回0,表示高度为0。

	if (node == NULL) return 0;

③确定每层递归的逻辑:
先求它的左子树的深度,再求的右子树的深度,最后取左右深度最大的数值 再+1 ( 加1是因为算上当前节点),就是目前节点为根节点的树的深度。
按照后序遍历(左、右、中)去遍历二叉树。

	int leftDepth = getDepth(node->left);       // 左
	int rightDepth = getDepth(node->right);     // 右
	int depth = 1 + max(leftDepth, rightDepth); // 中
	return depth;

完整代码:

	class Solution {
	public:
	    int getDepth(TreeNode* node) {
	        if (node == NULL) return 0;
	        int leftDepth = getDepth(node->left);       // 左
	        int rightDepth = getDepth(node->right);     // 右
	        int depth = 1 + max(leftDepth, rightDepth); // 中
	        return depth;
	    }
	    int maxDepth(TreeNode* root) {
	        return getDepth(root);
	    }
	};

当然,上一篇文章不是说了可以化繁为简嘛~
在这里插入图片描述

精简代码如下:

	class Solution {
	public:
	    int maxDepth(TreeNode* root) {
	        if (root == NULL) return 0;
	        return 1 + max(maxDepth(root->left), maxDepth(root->right));
	    }
	};

当然,这在逻辑上也看不出来是二叉树的哪种遍历了。

2、迭代法

使用迭代法的话, 层序遍历是最为合适,因为最大的深度就是二叉树的层数。、
直接抄前面博客写的二叉树层序模板就行了 。

	class Solution {
	public:
	    int maxDepth(TreeNode* root) {
	        if (root == NULL) return 0;
	        int depth = 0;
	        queue<TreeNode*> que;
	        que.push(root);
	        while(!que.empty()) {
	            int size = que.size();
	            depth++; // 记录深度
	            for (int i = 0; i < size; i++) {
	                TreeNode* node = que.front();
	                que.pop();
	                if (node->left) que.push(node->left);
	                if (node->right) que.push(node->right);
	            }
	        }
	        return depth;
	    }
	};

学会了求解二叉树的最大深度,也就可以求解N叉树的最大深度。

leetcode 559 N叉树的最大深度

传送门
在这里插入图片描述

思路

不用多说,跟leetcode 104一样,递归迭代都可以。迭代法还是采用层序遍历。
我就直接贴代码了。

1、递归法

class Solution {
public:
    int maxDepth(Node* root) {
        if (root == 0) return 0;
        int depth = 0;
        for (int i = 0; i < root->children.size(); i++) {
            depth = max (depth, maxDepth(root->children[i]));
        }
        return depth + 1;
    }
};

2、迭代法

class Solution {
public:
    int maxDepth(Node* root) {
        queue<Node*> que;
        if (root != NULL) que.push(root);
        int depth = 0; 
        while (!que.empty()) {
            int size = que.size();
            vector<int> vec;
            depth++; // 记录深度
            for (int i = 0; i < size; i++) {
                Node* node = que.front();
                que.pop();
                for (int j = 0; j < node->children.size(); j++) {
                    if (node->children[j]) que.push(node->children[j]);
                }
            }
        }
        return depth;
    }
};

学会这些题,大概就理解 自古真情留不住,唯有套路得人心了吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓梦林

都看到这里了,支持一下作者呗~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值