LeetCode:二叉树的属性(二叉树的最大最小深度)

LeetCode:二叉树的属性(二叉树的最大最小深度)

LeetCode 二叉树刷题相关教程:

划重点: 使用前序求的就是深度,使用后序求的是高度。根节点的高度就是二叉树的最大深度。

104. 二叉树的最大深度

给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

class Solution {
public:
	int maxDepth(TreeNode* root) {
		return getDepth(root);
	}

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

	void getDepth(TreeNode* node, int depth) {
		result = depth > result ? depth : result; // 中
		if (node->left == nullptr && node->right == nullptr) {
			return;
		}

		if (node->left) {
			getDepth(node->left, depth + 1);
		}
		if (node->right) {
			getDepth(node->right, depth + 1);
		}
		return;
	}

	int result = 0;
};

使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。

class Solution {
public:
	int maxDepth(TreeNode* root) {
		int depth = 0;
		queue<TreeNode*> que;
		if (root != nullptr) 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;
	}
};

559. N 叉树的最大深度

给定一个 N 叉树,找到其最大深度。最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔。

class Solution {
public:
    int maxDepth(Node* root) {
        return getDepth(root);
    }

    int getDepth(Node* node) {
        int depth = 0;
        if (node == nullptr) return 0;
        /*for (vector<Node*>::iterator it = node->children.begin(); it != node->children.end(); it++) {
            depth = max(getDepth(*it), depth);
        }*/
        
        for (int i = 0; i < node->children.size(); i++) {
            depth = max(getDepth(node->children[i]), depth);
        }
        return depth + 1;
    }
};
class Solution {
public:
    int maxDepth(Node* root) {
        if (root == nullptr) return 0;
        queue<Node*> que;
        int depth = 0;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            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;
    }
};

111. 二叉树的最小深度

给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量

class Solution {
public:
    int minDepth(TreeNode* node) {
		if (node == nullptr) return 0;
		if (node->left && !node->right) { // 当一个左子树为空,右不为空,这时并不是最低点
			return 1 + minDepth(node->left);
		}
		if (!node->left && node->right) { // 当一个右子树为空,左不为空,这时并不是最低点
			return 1 + minDepth(node->right);
		}
		return 1 + min(minDepth(node->left), minDepth(node->right));
	}
};

迭代法:

class Solution {
public:
	int minDepth(TreeNode* root) {
		int depth = 0;
		queue<TreeNode*> que;
		if (root != nullptr) 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);
				if (!node->left && !node->right) return depth;
			}
		}
		return depth;
	}
};

以上题解大多来自【代码随想录】,在此基础上做了一定总结,并附带一些自己的理解。

后续题目,随缘更新,有错误请指出!

END

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

奋斗的西瓜瓜

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值