二叉树常见题目2
1.[Lang] 静态变量2.[Lang] Lambda表达式3.[Lang] 类成员修饰符4.[Lang] 构造与析构5.[Lang] 智能指针6.[Lang] 运算符重载7.[Lang] 虚函数8.[Lang] 函数模板9.[Lang] 类模板10.二分查找11.链表操作12.栈和队列13.二叉树遍历14.归并分治15.快速排序16.堆结构17.位运算18.链表操作219.二叉树常见题目20.前缀树21.前缀和
22.二叉树常见题目2
23.二维差分24.滑动窗口25.双指针26.二分答案法27.单调栈28.单调队列29.并查集30.洪水填充31.一维动态规划32.二维动态规划33.二维动态规划234.三维动态规划35.子数组最大累加和36.子数组最大累加和237.零一背包38.分组背包和完全背包39.多重背包40.区间dp[Algo] 二叉树常见题目2
1. 最近公共祖先LCA
BinaryTreeNode *LCA(BinaryTreeNode *root, BinaryTreeNode *a, BinaryTreeNode *b)
{
if (root == nullptr || root == a || root == b) return root;
BinaryTreeNode *l = LCA(root->left, a, b), *r = LCA(root->right, a, b);
if (l == nullptr && r == nullptr) return nullptr;
if (l != nullptr && r != nullptr) return root;
return l == nullptr ? r : l;
}
2. 累加和为aim的所有路径
vector<vector<int>> path_list; //最终结果
vector<int> cur_path;
int cur_sum;
// 2. 累加和为aim的所有路径
void sumPath(BinaryTreeNode *root, int aim)
{
if (root == nullptr) return;
if (root->left == nullptr && root->right == nullptr)
{
// 当前节点是叶节点
if (cur_sum + root->val == aim)
{
cur_path.push_back(root->val);
path_list.push_back(cur_path);
cur_path.pop_back();
}
}
else
{
// 非叶节点
cur_sum += root->val;
cur_path.push_back(root->val);
sumPath(root->left, aim);
sumPath(root->right, aim);
cur_sum -= root->val;
cur_path.pop_back();
}
}
3. 判断平衡二叉树
struct retStruct1
{
int h; // 树的高度
bool flag; // 是否是平衡二叉树
};
retStruct1 process1(BinaryTreeNode *root)
{
if (root == nullptr) return retStruct1{0, true};
retStruct1 ret_l = process1(root->left);
retStruct1 ret_r = process1(root->right);
retStruct1 ret;
ret.flag = ret_l.flag && ret_r.flag && abs(ret_l.h - ret_r.h) <= 1;
ret.h = max(ret_l.h, ret_r.h) + 1;
return ret;
}
// 3. 判断平衡二叉树
bool isBBT(BinaryTreeNode *root)
{
return process1(root).flag;
}
4. 判断搜索二叉树
struct retStruct2
{
long long max; // 最大值
long long min; // 最小值
bool flag; // 是否是搜索二叉树
};
retStruct2 process2(BinaryTreeNode *root)
{
if (root == nullptr) return retStruct2{INT64_MIN, INT64_MAX, true};
retStruct2 ret_l = process2(root->left);
retStruct2 ret_r = process2(root->right);
retStruct2 ret;
ret.max = max(max(ret_l.max, ret_r.max), (long long)root->val);
ret.min = min(min(ret_l.min, ret_r.min), (long long)root->val);
ret.flag = ret_l.flag && ret_r.flag && ret_l.max < root->val && ret_r.min > root->val;
return ret;
}
// 4. 判断搜索二叉树
bool isBST(BinaryTreeNode *root)
{
return process2(root).flag;
}
5. 打家劫舍
struct retStruct3
{
int withMax; // 包含当前节点的最大值
int withoutMax; // 不包含当前节点的最大值
};
retStruct3 process3(BinaryTreeNode *root)
{
if (root == nullptr) return retStruct3{0, 0};
retStruct3 ret_l = process3(root->left);
retStruct3 ret_r = process3(root->right);
return retStruct3{root->val + ret_l.withoutMax + ret_r.withoutMax, max(ret_l.withMax, ret_l.withoutMax) + max(ret_r.withMax, ret_r.withoutMax)};
}
// 5. 打家劫舍
int rob(BinaryTreeNode *root)
{
retStruct3 result = process3(root);
return max(result.withMax, result.withoutMax);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现