104.二叉树的最大深度
题目链接:104. 二叉树的最大深度 - 力扣(LeetCode)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int GetDepth(TreeNode* Cur) {
if (Cur == nullptr) return 0;
int Left = GetDepth(Cur->left);
int Right = GetDepth(Cur->right);
return 1 + max(Left, Right);
}
int maxDepth(TreeNode* root) {
return GetDepth(root);
}
};
111.二叉树的最小深度
题目链接:111. 二叉树的最小深度 - 力扣(LeetCode)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int GetMin(TreeNode* Cur) {
if (Cur == nullptr) return 0;
int left = GetMin(Cur->left);
int right = GetMin(Cur->right);
if (Cur->left != nullptr && Cur->right == nullptr)
return 1 + left;
if (Cur->left == nullptr && Cur->right != nullptr)
return 1 + right;
return 1 + min(left, right);
}
int minDepth(TreeNode* root) {
return GetMin(root);
}
};
222.完全二叉树的节点个数文章来源:https://www.toymoban.com/news/detail-670635.html
题目链接:222. 完全二叉树的节点个数 - 力扣(LeetCode)文章来源地址https://www.toymoban.com/news/detail-670635.html
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int GetCount(TreeNode* Cur) {
if (Cur == nullptr) return 0;
int left = GetCount(Cur->left);
int right = GetCount(Cur->right);
return 1 + left + right;
}
int countNodes(TreeNode* root) {
return GetCount(root);
}
};
到了这里,关于第十五天|104.二叉树的最大深度、111.二叉树的最小深度、 222.完全二叉树的节点个数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!