力扣刷题||二叉树 L104.二叉树的最大深度 、L559.n叉树的最大深度、L111.二叉树的最小深度、L222.完全二叉树的节点个数

本天的任务权重在于掌握递归的方法,通过对对四题的练习,与观看卡哥的解题思路,慢慢的掌握了递归三部曲,其中最重要的就是递归三部曲的第一步,终止条件如何写。

http://104.二叉树的最大深度https://leetcode.cn/problems/maximum-depth-of-binary-tree/

如本题,求二叉树的最大深度

理解深度与高度的概念,深度从根节点到底层叶子结点,而高度则相反,采用后序遍历,可以遍历其深度,也就是从底层一层一层往上遍历,并且每个节点的位置,是叶子结点深度+1,这样就能最后返回最大深度,并且是判断每个节点左子树与右子树的最大值,并且可以采用层次遍历也一样,遍历到第几层则,就+1

层次遍历

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

递归遍历:与卡哥的一样就不放了,具体可看https://programmercarl.com/0104.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%A4%A7%E6%B7%B1%E5%BA%A6.html#%E9%80%92%E5%BD%92%E6%B3%95

http://L559.n叉树的最大深度https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/

采用递归遍历,分别遍历根节点中的孩子数组,并在返回时+1,以便返回根节点高度

class Solution {
public:
    int getmax(Node* root) {
        if (root == NULL)return 0;
        int result = 0;
        for (int i = 0; i < root->children.size(); i++)
        {
            int depth = getmax(root->children[i]);
            result = max(result, depth);//遍历完根节点的每一个节点之后,再进行加一,则代表,返回的高度
        }
        return result + 1;
    }
    int maxDepth(Node* root) {
        int result = getmax(root);
        return result;
    }
};

L111.二叉树的最小深度https://leetcode.cn/problems/minimum-depth-of-binary-tree/

本题中,求解二叉树的最小深度,也就是如何判断递归三部曲中的终止条件是一个难点,

1、肯定会有若为空则返回0

其中要考虑,左子树可能为空,但是右子树不为空,则最小深度并不是1,则需要分别进行讨论,也就是左子树有空,右子树不为空,那深度就是右子树+1,左子树也一样,这样依次寻找最小的值,递归遍历,采用后序遍历的方式,重点理解递归

class Solution {
public:
	int getmindepth(TreeNode* root)
	{
		if (root == NULL)return 0;
		int leftdepth = getmindepth(root->left);
		int rightdepth = getmindepth(root->right);
		if (root->left == NULL && root->right != NULL)
			return rightdepth + 1;
		if(root->left != NULL && root->right == NULL)
			return leftdepth + 1;
		int result = 1 + min(leftdepth, rightdepth);
		return result;
	}
	int minDepth(TreeNode* root) {
	return getmindepth(root);
    }
 };

 L222.完全二叉树的节点个数https://leetcode.cn/problems/count-complete-tree-nodes/

统计完全二叉树节点个数,完全可以采用层序遍历,但是主要学习递归遍历

1、分别统计到达最左边叶子节点的深度,和右边叶子节点的深度,判断两者是否相等,若相等,则完全二叉树的节点个数为2h-1,根据公式计算即可

2、若不想等,就将左右访问到的节点相加之后再加1,这样就能判断出有多少叶子节点。

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while (left) {  // 求左子树深度
            left = left->left;
            leftDepth++;
        }
        while (right) { // 求右子树深度
            right = right->right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

主要是理解二叉树的遍历顺序,与递归的含义,要是不理解,可以直接从二叉树最低端的某个节点进行一个试用判断,这样便于理解,这样一个一个往上走,这样便于理解,并且理解终止条件,可以先写三部曲的框架,在一步一步去写每个阶段的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值