总链接:day 16 第六章二叉树
104.二叉树的最大深度
链接:代码随想录
/**
* 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 maxDepth(TreeNode* root) {
if(root==nullptr)return 0;
else
{
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
}
};
559.n叉树的最大深度
链接:力扣
/** * 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 maxDepth(TreeNode* root) { if(root==nullptr)return 0; else { return max(maxDepth(root->left),maxDepth(root->right))+1; } } };
111.二叉树的最小深度
链接:代码随想录
注:这道题我用了回溯法,但并不是最佳解法。这种想法把空左子树、空右子树设为了最大。
/** * 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 minDepth(TreeNode* root) { if(root==nullptr) { return 0; } else if(root->right==nullptr && root->left==nullptr) { return 1; } return Dfs(root); } int Dfs(TreeNode *root) { if(root==nullptr) { return 100005; } if(root!=nullptr && root->left==nullptr && root->right==nullptr) { return 1; } int l=Dfs(root->left); int r=Dfs(root->right); return min(l,r)+1; } };
分情况讨论,一次递归,无需回溯。
/** * 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 minn=10e5+1; int depth=0; int minDepth(TreeNode* root) { if(root==nullptr) { return 0; } else { helper(root); return minn+1; } } void helper(TreeNode *root) { if(root==nullptr) { return ; } else if(root->left==nullptr && root->right==nullptr) { if(depth<minn) { minn=depth; } } else { depth++; helper(root->left); helper(root->right); depth--; } } };
最解解法,后序遍历
class Solution { public: int getDepth(TreeNode* node) { if (node == NULL) return 0; int leftDepth = getDepth(node->left); // 左 int rightDepth = getDepth(node->right); // 右 // 中 // 当一个左子树为空,右不为空,这时并不是最低点 if (node->left == NULL && node->right != NULL) { return 1 + rightDepth; } // 当一个右子树为空,左不为空,这时并不是最低点 if (node->left != NULL && node->right == NULL) { return 1 + leftDepth; } int result = 1 + min(leftDepth, rightDepth); return result; } int minDepth(TreeNode* root) { return getDepth(root); } };
222.完全二叉树的节点个数
链接:代码随想录
注:这道题完全忘记了怎么优化!!
/** * 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) { if(root==nullptr)return 0; 沿最左侧和最右侧分别计算高度 TreeNode *l=root; int leftdepth=0; TreeNode *r=root; int rightdepth=0; while(l!=nullptr) { l=l->left; leftdepth++; } while(r!=nullptr) { r=r->right; rightdepth++; } // 如果左右侧计算的高度相同,则是一棵满二叉树 if(leftdepth==rightdepth) { return pow(2,leftdepth)-1; } // 如果左右侧的高度不同,则按照普通二叉树的逻辑计算 return countNodes(root->left)+countNodes(root->right)+1; } };