摘要: #include <iostream> using namespace std; typedef struct node { int data; node* next; node() :data(0), next(nullptr) {} node(int x) :data(x), next(null 阅读全文
posted @ 2024-04-07 11:28 wshidaboss 阅读(19) 评论(0) 推荐(0) 编辑
摘要: //返回每一层二叉树的平均值(广度优先搜索,队列) 层序遍历 vector<double> averageOfLevels(TreeNode* root) { vector<double> ans; if (!root) return ans; queue<TreeNode*> q; q.push( 阅读全文
posted @ 2024-03-06 11:36 wshidaboss 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 四步法: (1)如果两个子树都为空指针,则它们相等或对称 (2)如果两个子树只有一个为空指针,则它们不相等或不对称 (3)如果两个子树根节点的值不相等,则它们不相等或不对称 (4)根据相等或对称要求,进行递归处理。 //四步法判断一颗二叉树是否对称 //主函数 bool isSymmetric(Tr 阅读全文
posted @ 2024-03-06 11:34 wshidaboss 阅读(6) 评论(0) 推荐(0) 编辑
摘要: //使用递归翻转二叉树 TreeNode* reverseTree(TreeNode* root) { if (!root) return root; swap(root->left, root->right); reverseTree(root->left); reverseTree(root-> 阅读全文
posted @ 2024-03-06 11:31 wshidaboss 阅读(3) 评论(0) 推荐(0) 编辑
摘要: //求一颗二叉树的最大深度 求高度:后序遍历 求深度:前序遍历 int maxDepth(TreeNode* root) { return root ? 1 + max(maxDepth(root->left), maxDepth(root->right)) : 0; } //求一颗二叉树的最小深度 阅读全文
posted @ 2024-03-06 11:30 wshidaboss 阅读(10) 评论(0) 推荐(0) 编辑
摘要: #include <vector> #include <iostream> #include <string> using namespace std; //二叉树的定义 struct TreeNode { int val; TreeNode* left; TreeNode* right; Tree 阅读全文
posted @ 2024-03-06 11:29 wshidaboss 阅读(6) 评论(0) 推荐(0) 编辑
摘要: Mat和Pat希望邀请他们的朋友来参加派对。他们要编写一个程序完成下面的任务。 让Mat输入他朋友的姓名列表。姓名存储在一个容器中,然后按排列后的顺序显示出来。让Pat输入她朋友的姓名列表。姓名存储在另一个容器中,然后按排列后的顺序显示出来。创建第三个容器,将两个列表合并,删除重复的部分,并显示这个 阅读全文
posted @ 2024-02-29 11:27 wshidaboss 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 相对于数组,在链表中添加和删除元素更容易,但排序速度更慢。这就引出了一种可能性:相对于使用链表算法进行排序,将链表复制到数组中,对数组进行排序,再将排序后的结果复制到链表中的速度可能更快;但这也可能占用更多的内存。请使用如下方法检验上述假设。 a.创建大型vector<int>对象vi0,并使用ra 阅读全文
posted @ 2024-02-29 10:27 wshidaboss 阅读(2) 评论(0) 推荐(0) 编辑
摘要: #include <iostream> #include <string> using namespace std; int reduce(long arr[], int n) { sort(arr, arr + n); auto str = unique(arr, arr + n); return 阅读全文
posted @ 2024-02-21 17:47 wshidaboss 阅读(1) 评论(0) 推荐(0) 编辑
摘要: //判断字符串是否是回文字符串(考虑大小写,空格和标点符号) bool palindrome1(string& str) { string ret; for (auto& c : str) { if (isalpha(c)) { if (isupper(c)) { ret.push_back(tol 阅读全文
posted @ 2024-02-20 18:37 wshidaboss 阅读(13) 评论(0) 推荐(0) 编辑