今日任务
226.翻转二叉树
- 题目链接: https://leetcode.cn/problems/invert-binary-tree/description/
- 题目描述:
Code 递归 Or 迭代
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == nullptr){
return nullptr;
}
// TreeNode *left = invertTree(root->left);
// TreeNode *right = invertTree(root->right);
// root->left = right;
// root->right = left;
// invertTree(root->right);
// swap(root->left, root->right);
// invertTree(root->right);
vector<TreeNode *> cur = {root};
while(!cur.empty()){
vector<TreeNode *> next;
for(auto &node : cur){
swap(node->left, node->right);
if(node->left){
next.emplace_back(node->left);
}
if(node->right){
next.emplace_back(node->right);
}
}
cur = move(next);
}
return root;
}
};
101. 对称二叉树
- 题目链接: https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
- 题目描述:
Code 迭代 Or 递归
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root == nullptr){
return true;
}
// function<bool(const vector<TreeNode*> &)> is_symmetry =
// [&](const vector<TreeNode *> &cur) ->bool{
// int left = 0, right = cur.size() - 1;
// while(left < right){
// if(cur[left] && cur[right]){
// if(cur[left]->val != cur[right]->val){
// return false;
// }
// }else if(cur[left] != cur[right]){
// return false;
// }
// left++;
// right--;
// }
// return true;
// };
// vector<TreeNode *> cur = {root};
// while(!cur.empty()){
// vector<TreeNode *> next;
// for(const auto &node : cur){
// if(node){
// next.emplace_back(node->left);
// next.emplace_back(node->right);
// }
// }
// if(!is_symmetry(next)){
// return false;
// }
// cur = move(next);
// }
// return true;
function <bool(TreeNode *, TreeNode *)> is_symmetry
= [&](auto p, auto q)->bool{
if(p == nullptr || q == nullptr){
return p == q;
}
return p->val == q->val && is_symmetry(p->left, q->right) && is_symmetry(p->right, q->left);
};
return is_symmetry(root->left, root->right);
}
};
104. 二叉树的最大深度
- 题目链接: https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/
- 题目描述:
Code
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == nullptr){
return 0;
}
int lmax = maxDepth(root->left);
int rmax = maxDepth(root->right);
return 1 + max(lmax, rmax);
}
};
111. 二叉树的最小深度
- 题目链接: https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
- 题目描述:
-
Code 三种递归
class Solution {
public:
int minDepth(TreeNode* root) {
// if(root == nullptr){
// return 0;
// }
// int ldepth = minDepth(root->left);
// int rdepth = minDepth(root->right);
// return ldepth && rdepth ? min(ldepth, rdepth) + 1 : 1 + ldepth + rdepth;
// int ans = INT_MAX;
// function<void(TreeNode *, int)> dfs = [&] (TreeNode *node, int depth) -> void{
// if(node == nullptr || ++depth >= ans){
// return ;
// }
// if(node->left == node->right){
// ans = depth;
// return;
// }
// dfs(node->left, depth);
// dfs(node->right, depth);
// };
// dfs(root, 0);
// return root ? ans : 0;
if(root == nullptr){
return 0;
}
if(root->right == nullptr){
return 1 + minDepth(root->left);
}
if(root->left == nullptr){
return 1 + minDepth(root->right);
}
return 1 +min(minDepth(root->left), minDepth(root->right));
}
};