DFS注意char(int + char) + string如何进行int->char+string连接

988. Smallest String Starting From Leaf

Medium

11115FavoriteShare

Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to 'z': a value of 0represents 'a', a value of 1 represents 'b', and so on.

Find the lexicographically smallest string that starts at a leaf of this tree and ends at the root.

(As a reminder, any shorter prefix of a string is lexicographically smaller: for example, "ab" is lexicographically smaller than "aba".  A leaf of a node is a node that has no children.)

 

Example 1:

Input: [0,1,2,3,4,3,4]
Output: "dba"

Example 2:

Input: [25,1,3,1,3,0,2]
Output: "adz"

Example 3:

Input: [2,2,1,null,1,0,null,0]
Output: "abc"
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    string smallestFromLeaf(TreeNode* root) {
        vector<string> Result;
        if(root == NULL){return "";}
        DFS(root,"",Result);
        sort(Result.begin(),Result.end());
        return Result[0];
    }
    void DFS(TreeNode* root,string Path,vector<string>& Result){
        if(root->left == NULL && root->right == NULL){
            Result.push_back(char(root->val + 'a') + Path);
        }
        if(root->left != NULL){
            DFS(root->left,char(root->val + 'a') + Path,Result);
        }
        if(root->right != NULL){
            DFS(root->right,char(root->val + 'a') + Path,Result);
        }
    }
};

 

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #define MAXV 1000 #define ElemType int #define INF 32767typedef struct { int no; int info; }VertexType; typedef struct{ int edges[MAXV][MAXV]; int n,e; VertexType vexs[MAXV]; }MatGraph; typedef struct ArcNode{ int adjvex; int weight; struct ArcNode *nextarc; }ArcNode; typedef struct VNode{ VertexType data; ArcNode *firstarc; }VNode,AdjList[MAXV]; typedef struct{ AdjList adjlist; int n,e; }AdjGraph; void CreateAdj(AdjGraph *&G,int A [MAXV][MAXV],int n,int e){ int i,j;ArcNode *p; G=(AdjGraph *)malloc(sizeof(AdjGraph)); for(i=0;i<n;i++) { G->adjlist[i].firstarc=NULL; } for(i=0;i<n;i++) { for(j=n-1;j>=0;j--) { if(A[i][j]!=0 && A[i][j]!=INF) { p=(ArcNode *)malloc(sizeof(ArcNode)); p->adjvex=j; p->weight=A[i][j]; p->nextarc=G->adjlist[i].firstarc; G->adjlist[i].firstarc=p; } } } G->n=n;G->e=e; }void DispAdj(AdjGraph *G) { int i;ArcNode *p; for(i=0;i<G->n;i++) { p=G->adjlist[i].firstarc; printf("%3d:",i); while(p!=NULL) { printf("%3d[%d]->",p->adjvex,p->weight); p=p->nextarc; } printf("^\n"); } }typedef struct{ int data[MAXV]; int front,rear; }SqQueue; void InitQueue(SqQueue *&q){ q=(SqQueue *)malloc(sizeof(SqQueue)); q->front=q->rear=-1; } void DestroyQueue(SqQueue *&q){ free(q); } bool QueueEmpty(SqQueue *q){ return q->front == q->rear; } bool enQueue(SqQueue *&q,int e){ if(q->rear ==MAXV -1){ return false; } q->rear++; q->data[q->rear]=e; return true; } bool deQueue(SqQueue *&q,int &e){ if(q->front ==q->rear){ return false; } q->front++; e=q->data[q->front]; return true; }MatGraph *CreateMat(char a[],int n,int e) { MatGraph *G=(MatGraph *)malloc(sizeof(MatGraph)); int i,j,k; G->n=n; G->e=e; for(i=0;i<n;i++) { G->vexs[i].no=i; G->vexs[i].info=a[i]; } for(i=0;i<n;i++) { for(j=0;j<n;i++) { G->edges[i][j]=0; } } for(k=0;k<e;k++) { printf("输入相邻的顶点:"); scanf("%d",&i); G->edges[i][j]=1; G->edges[j][i]=1; } return G; } int main(){ int n=7,e=12; char a[]={'0','1','2','3','4','5','6'}; MatGraph *G=CreateMat(a,n,e); AdjGraph *H; CreateAdj(H,G->edges,n,e); DFS(G,v); return 0; }修改上述代码
05-17
#pragma GCC optimize ("O3") #pragma pack (16)//所有的存储都是以16个字节为单位的 #include <stdio.h> #include <stdlib.h> #include <string.h> #define tolower(c) (c>='A'&&c<='Z')?c-'A'+'a':c #define DATA 5200000 #define SIZE 1000005 int trie[4200000][26]; typedef struct node { int cnt; int logo; struct node *child[26]; } Node; Node *root; char str[35000000]; typedef struct word { char wor[85]; int cnt; } Word; Word w[300000]; struct node *creat() { Node *Root = (Node *)malloc(sizeof(Node)); Root->logo = 0; Root->cnt = 0; for (int i = 0; i < 26; i++) { Root->child[i] = NULL; } return Root; } void insert(Node *root, char *word, int flag) { struct node *leaf = root; for (int i = 0; word[i] != '\0'; i++) { int index = word[i] - 'a'; if (!leaf->child[index]) { leaf->child[index] = creat(); } leaf = leaf->child[index]; } if (leaf->logo != -1) leaf->logo = flag; leaf->cnt++; } int count = 0; void dfs(Node *leaf, char *word, int level) { if (leaf->logo == 1) { word[level] = '\0'; strcpy(w[count++].wor, word); w[count - 1].cnt = leaf->cnt; } for (int i = 0; i < 26; i++) { if (leaf->child[i]) { word[level] = i + 'a'; dfs(leaf->child[i], word, level + 1); } } } int cmp(const void *p1, const void *p2) { Word *v1, *v2; v1 = (Word *)p1; v2 = (Word *)p2; if (v1->cnt != v2->cnt) return v2->cnt - v1->cnt; else return strcmp(v1->wor, v2->wor); } int main(int argc, char *argv[]) { char s[1024]; int temp; int n, m;//读入n,m; //n = atoi(argv[1]); //m = atoi(argv[2]); scanf("%d%d", &n, &m); //读入stopwords的元素,并令末序数组值为0,即该单词不计入 root = creat(); FILE *stopwords = fopen("stopwords.txt", "r"); while (fscanf(stopwords, "%s", s) != EOF) { insert(root, s, -1); } int cnt; FILE *article = fopen("article.txt", "r"); cnt = fread(str, sizeof(char), 35000000, article); char word[85]; int w_cnt = 0; for (int i = 0; i < cnt; i++) { char c = tolower(str[i]); if (c >= 'a' && c <= 'z') { word[w_cnt++] = c; } else { word[w_cnt] = '\0'; p = 0; w_cnt = 0; if (strlen(word) > 0) { insert(root, word, 1); } }//对article的所有单词进行计数 } dfs(root, word, 0); qsort(w, count, sizeof(w[0]), cmp); printf("%s", w[0].cnt); return 0; }
05-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值