110.平衡二叉树
// 平衡二叉树,理解稍微有点难度
#include <iostream>
#include <algorithm> // For std::abs and std::max functions
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 辅助函数,计算二叉树的高度,并检查是否平衡
int getHeight(TreeNode *root)
{
if (root == NULL)
return 0;
int leftHeight = getHeight(root->left);
if (leftHeight == -1)
return -1;
int rightHeight = getHeight(root->right);
if (rightHeight == -1)
return -1;
return abs(leftHeight - rightHeight) > 1 ? -1 : std::max(leftHeight, rightHeight) + 1;
}
// 主函数,判断二叉树是否平衡
bool isBalanced(TreeNode *root)
{
return getHeight(root) == -1 ? false : true;
}
int main()
{
// 构建一个简单的二叉树作为例子
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->left->left = new TreeNode(3); // 添加深层左子树来制造不平衡
root->right = new TreeNode(4);
// 检查是否平衡
bool result = isBalanced(root);
std::cout << "Is the tree balanced? " << (result ? "Yes" : "No") << std::endl;
return 0;
}
257. 二叉树的所有路径
#include <iostream>
#include <vector>
#include <string>
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 辅助函数,用于递归遍历并收集路径,注意传参当前路径也可以用引用,相当于添加了回溯过程
void dfs(TreeNode *node, std::vector<std::string> &paths, std::string currentPath)
{
if (node == nullptr)
{
return; // 如果节点为空,直接返回
}
// 将当前节点值添加到路径中,注意要在叶子节点的前面
currentPath += std::to_string(node->val);
// 如果是叶子节点,将路径添加到结果集中
if (node->left == nullptr && node->right == nullptr)
{
paths.push_back(currentPath);
}
else
{
// 如果不是叶子节点,继续递归遍历
currentPath += "->"; // 使用箭头作为节点分隔符
dfs(node->left, paths, currentPath);
dfs(node->right, paths, currentPath);
}
}
// 主函数,返回所有从根节点到叶子节点的路径
std::vector<std::string> binaryTreePaths(TreeNode *root)
{
std::vector<std::string> paths;
dfs(root, paths, "");
return paths;
}
int main()
{
// 构建一个简单的二叉树作为例子
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->right = new TreeNode(5);
// 收集所有路径
std::vector<std::string> paths = binaryTreePaths(root);
// 打印所有路径
for (const std::string &path : paths)
{
std::cout << path << std::endl;
}
return 0;
}
404.左叶子之和
#include <iostream>
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 辅助函数,用于递归遍历并检测左叶子节点
int sumOfLeftLeaves(TreeNode *node, bool isLeft)
{
if (node == nullptr)
{
return 0; // 如果节点为空,返回0
}
// 如果当前节点是叶子节点并且是左节点
if (node->left == nullptr && node->right == nullptr && isLeft)
{
return node->val;
}
// 递归计算左子树和右子树的左叶子之和
return sumOfLeftLeaves(node->left, true) + sumOfLeftLeaves(node->right, false);
}
// 另外一种写法在节点上一级操作更加简洁
int sumOfLeftLeaves2(TreeNode *root)
{
if (root == NULL)
return 0;
int leftValue = 0;
if (root->left != NULL && root->left->left == NULL && root->left->right == NULL)
{
leftValue = root->left->val;
}
return leftValue + sumOfLeftLeaves2(root->left) + sumOfLeftLeaves2(root->right);
}
// 主函数,返回所有左叶子之和
int sumOfLeftLeaves(TreeNode *root)
{
return sumOfLeftLeaves2(root);
}
int main()
{
// 构建一个简单的二叉树作为例子
TreeNode *root = new TreeNode(3);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);
// 计算所有左叶子节点之和
std::cout << "Sum of all left leaves: " << sumOfLeftLeaves(root) << std::endl;
return 0;
}
222.完全二叉树的节点个数
// 完全二叉树的节点个数
#include <iostream>
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 计算树的高度
int computeHeight(TreeNode *node)
{
int height = 0;
while (node != nullptr)
{
height++;
node = node->left; // 移动到左子树
}
return height;
}
// 计算完全二叉树的节点总数
int countNodes(TreeNode *root)
{
if (root == nullptr)
{
return 0;
}
int leftHeight = computeHeight(root->left);
int rightHeight = computeHeight(root->right);
if (leftHeight == rightHeight)
{
// 左子树是满的
return (1 << leftHeight) + countNodes(root->right);
}
else
{
// 右子树是满的
return (1 << rightHeight) + countNodes(root->left);
}
}
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);
// 计算完全二叉树的节点总数
std::cout << "Total number of nodes: " << countNodes(root) << std::endl;
return 0;
}
标签:right,TreeNode,int,随想录,二叉树,new,15,root,left
From: https://blog.csdn.net/weixin_48850397/article/details/141395569