二叉树深度:
#include
#include
#include
#include
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 创建二叉树
TreeNode* createTree(const vector& nodes, int index) {
if (index >= nodes.size() || nodes[index] == "null") {
return nullptr;
}
int val = stoi(nodes[index]);
TreeNode* root = new TreeNode(val);
root->left = createTree(nodes, 2 * index + 1);
root->right = createTree(nodes, 2 * index + 2);
return root;
}
// 计算树的高度
int getHeight(TreeNode* root) {
if (root == nullptr) {
return 0;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return 1 + max(leftHeight, rightHeight);
}
// 判断是否是平衡二叉树
bool isBalanced(TreeNode* root) {
if (root == nullptr) {
return true;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
if (abs(leftHeight - rightHeight) <= 1 && isBalanced(root->left) && isBalanced(root->right)) {
return true;
}
return false;
}
int main() {
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
string input;
getline(cin, input);
stringstream ss(input);
vector nodes;
string node;
while (ss >> node) {
nodes.push_back(node);
}
TreeNode* root = createTree(nodes, 0);
bool balanced = isBalanced(root);
if (balanced) {
cout << "True" << endl;
}
else {
cout << "False" << endl;
}
return 0;
}
【问题描述】
给定一个字符串,请将字符串里的字符按照出现的频率降序排列。例如,输入"tree",输出结果为"eert"。输入"Aabb",输出结果为"bbAa"。要求设计满足题目条件的如下函数:
class Solution { public: string frequencySort(string s) { … } };
【输入形式】
一个字符串。其中包括键盘上的所有可打印字符,比如空格、大小写字母、数字、标点符号等。
【输出形式】
按字符出现频度由高到低的顺序排列的字符串。若字符出现的频度一样,则按ASCII码表中的顺序进行排列。
【样例输入】
tree
【样例输出】
eert
【样例说明】
b在原字符串中出现的频度最高,因此排列在最前面,字符r和t出现的频度一样,则按ASCII码表中的顺序进行排列。测试数据存放在in.txt文件中。
#include
#include
#include
#include
using namespace std;
class Solution {
public:
string frequencySort(string s) {
unordered_map freqMap;
// 统计字符出现的频率
for (char c : s) {
freqMap[c]++;
}
// 按字符频率降序排序
sort(s.begin(), s.end(), [&](char a, char b) {
if (freqMap[a] != freqMap[b]) {
return freqMap[a] > freqMap[b];
} else {
return a < b;
}
});
return s;
}
};
int main() {
string input;
cin >> input;
Solution solution;
string output = solution.frequencySort(input);
cout << output << endl;
return 0;
}
#include
#include
#include
#include
using namespace std;
struct CharFrequency {
char character;
int frequency;
};
class Solution {
public:
static bool compare(const CharFrequency& a, const CharFrequency& b) {
if (a.frequency != b.frequency) {
return a.frequency > b.frequency;
} else {
return a.character < b.character;
}
}
string frequencySort(string s) {
vector freqList(128, {0, 0});
// 统计字符出现的频率
for (char c : s) {
freqList[c].character = c;
freqList[c].frequency++;
}
// 按字符频率降序排序
sort(freqList.begin(), freqList.end(), compare);
string result;
for (const CharFrequency& cf : freqList) {
result += string(cf.frequency, cf.character);
}
return result;
}
};
int main() {
string input;
cin >> input;
Solution solution;
string output = solution.frequencySort(input);
cout << output << endl;
return 0;
}
#include
#include
#include
#include
using namespace std;
struct Node
{
char ch;
int n;
Node(char ch) : ch(ch), n(0) {}
Node() : ch(' '), n(0) {}
};
bool compare(const Node& a, const Node& b)
{
if (a.n != b.n)
return a.n > b.n;
return a.ch < b.ch;
}
string frequencySort(string str)
{
vector freqList(128, Node());
// 统计字符出现的频率
for (char ch : str)
{
freqList[ch].ch = ch;
freqList[ch].n++;
}
// 按字符频率降序排序
sort(freqList.begin(), freqList.end(), compare);
string result;
for (const Node& node : freqList)
{
result += string(node.n, node.ch);
}
return result;
}
int main()
{
string str;
cin >> str;
string output = frequencySort(str);
cout << output << endl;
return 0;
}
#include
#include
#include
#include
using namespace std;
struct Node
{
char ch;
int n;
Node(char ch):ch(ch),n(1){}
Node():ch(' '),n(0){}
};
bool map( const Node &a,const Node &b)
{
if(a.n!=b.n)
return a.n>b.n;
return a.ch q;
for(char ch:str)
{
bool flag=0;
for(Node&n:q)
{
if(n.ch==ch)
{ n.n++;
flag=1;
break;
}
}
if(!flag)
q.push_back(Node(ch));
}
sort(q.begin(), q.end(),map);
for(Node n:q)
{
for(int i=0;i>str;
print(str);
return 0;
}