226翻转二叉树,代码如下
// 翻转二叉树
#include <iostream>
using namespace std;
struct Treenode
{
int val;
Treenode *left;
Treenode *right;
Treenode(int a) : val(a), left(nullptr), right(nullptr) {}
};
Treenode *invertTree(Treenode *root)
{
if (root == nullptr)
return root;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
// 辅助函数:打印二叉树的前序遍历
void printPreorder(Treenode *root)
{
if (root == nullptr)
{
return;
}
std::cout << root->val << " ";
printPreorder(root->left);
printPreorder(root->right);
}
int main()
{
// 创建一个简单的二叉树
Treenode *root = new Treenode(1);
root->left = new Treenode(2);
root->right = new Treenode(3);
root->left->left = new Treenode(4);
root->left->right = new Treenode(5);
root->right->left = new Treenode(6);
root->right->right = new Treenode(7);
std::cout << "Original tree (preorder): ";
printPreorder(root);
std::cout << std::endl;
// 翻转二叉树
Treenode *invertedRoot = invertTree(root);
std::cout << "Inverted tree (preorder): ";
printPreorder(invertedRoot);
std::cout << std::endl;
return 0;
}
101对称二叉树、代码如下
// 对称二叉树检查
#include <iostream>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool compare(TreeNode *left, TreeNode *right)
{
// 先排除节点为空和左右节点值不一样的情况
if (left == NULL && right != NULL)
return false;
else if (left != NULL && right == NULL)
return false;
else if (left == NULL && right == NULL)
return true;
else if (left->val != right->val)
return false;
// 此时左右节点值一样,需要判断下一层,开始进行迭代
bool outside = compare(left->left, right->right);
bool inside = compare(left->right, right->left);
bool isSame = outside && inside;
return isSame;
}
bool isSymmetric(TreeNode *root)
{
if (root == NULL)
return true;
return compare(root->left, root->right);
}
int main()
{
// 创建一个简单的二叉树
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(2);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->right->left = new TreeNode(5);
root->right->right = new TreeNode(4);
if (isSymmetric(root))
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
return 0;
}
104.二叉树的最大深度
#include <iostream>
#include <algorithm> // 用于 std::max
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 计算二叉树的最大深度
int maxDepth(TreeNode *root)
{
if (root == nullptr)
{
return 0; // 空树的深度是 0
}
// 递归计算左子树和右子树的最大深度
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
// 当前节点的深度等于较大的子树深度加1(加的1是当前节点这一层)
return std::max(leftDepth, rightDepth) + 1;
}
int main()
{
// 构建一个简单的二叉树作为例子
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
// 输出二叉树的最大深度
std::cout << "Max Depth of the tree: " << maxDepth(root) << std::endl;
return 0;
}
111.二叉树的最小深度
#include <iostream>
#include <algorithm> // For std::min function
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 计算二叉树的最小深度
int minDepth(TreeNode *root)
{
if (root == nullptr)
{
return 0; // 空树的深度是 0
}
// 如果左子树为空,递归计算右子树的最小深度
if (root->left == nullptr)
{
return minDepth(root->right) + 1;
}
// 如果右子树为空,递归计算左子树的最小深度
if (root->right == nullptr)
{
return minDepth(root->left) + 1;
}
// 如果两个子树都不为空,计算两个子树的最小深度,取较小值
int leftDepth = minDepth(root->left);
int rightDepth = minDepth(root->right);
return std::min(leftDepth, rightDepth) + 1;
}
int main()
{
// 构建一个简单的二叉树作为例子
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->left->right->left = new TreeNode(6);
// 输出二叉树的最小深度
std::cout << "Min Depth of the tree: " << minDepth(root) << std::endl;
return 0;
}