day31
【0111.二叉树的最小深度】
- 迭代法容易套模板 容易想到 递归法三部曲中第二步、第三步 都想不到
class Solution {
public:
int minDepth(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);
if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出
return depth;
}
}
}
return depth;
}
};
- 递归法 我的代码 见下
class Solution {
public:
int depth (TreeNode* root) {
int result1 = 1;
int result2 = 1;
if (root == NULL) return 0;
if (root->left != NULL && root->right == NULL ){
return depth(root->left) + 1;
}
if (root->right != NULL && root->left == NULL ){
return depth(root->right) + 1;
}
return result1 < result2 ? result1 + 1 : result2 + 1;
}
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
int result = depth (root);
return result;
}
};
- 上述我的代码是错误滴 参考答案见下
- 第一段代码 求最小深度的递归法
- 第二段代码 求最大深度的递归法
class Solution {
public:
int depth (TreeNode* root) {
if (root == NULL) return 0;
int result1 = depth(root->left);
int result2 = depth(root->right);
if (root->left != NULL && root->right == NULL ){
return result1 + 1;
}
if (root->right != NULL && root->left == NULL ){
return result2 + 1;
}
//return result1 < result2 ? result1 + 1 : result2 + 1;
int result = 1 + min(result1, result2);
return result;
}
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
int result = depth (root);
return result;
}
};
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);
}
};
- 第一 要会调用
min函数和max函数 - 最重要的 是求最大深度的代码 并不是把max变成min就可以实现求最小深度了 还需要 添加两个if判断 为什么呢 因为 如果不添加判断 当最初的二叉树根的左子树和右子树其中一个为空的话 就会直接返回当前深度0+1 = 1 表示最小深度为1 但是这只是一个根节点的高度 并不符合题目要求的 根到叶节点的最小距离 因此需要添加两个if规避这种情况 ----------- 如果左子树为空 那么就去在右子树里找最小深度 同样的 如果右子树为空的话 就在左子树找最小深度
【0222.完全二叉树的节点个数】
-
普通二叉树均适用的方法
- 迭代法
- 递归法
-
完全二叉树御用的方法
-
迭代法--------求二叉树深度的代码简单改一下
/**
* 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 countNodes(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
int result = 0;
while (!que.empty()) {
int size = que.size();
for (int i = 0; i <size; i++) {
TreeNode* node = que.front();
que.pop();
result++;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return result;
}
};
- 递归法-------求二叉树深度的代码简单改一下
/**
* 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* root) {
if (root == NULL) return 0;
int leftCount = getDepth(root->left);
int rightCount = getDepth(root->right);
int result = leftCount + rightCount + 1;
return result;
}
int countNodes(TreeNode* root) {
int result = getDepth(root);
return result;
}
};
- 完全二叉树御用特有的方法----递归法---暂时没看懂下次看
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;
}
};
本文来自博客园,作者:跬步瑶,转载请注明原文链接:https://www.cnblogs.com/deservee/p/16950629.html

浙公网安备 33010602011771号