1130 Infix Expression

news/2024/7/1 10:29:03

考察:DFS进行中序遍历。

注意:给除了根节点以外的父节点加左右括号。

AC代码

#include<cstdio>
#include<iostream>
#include<set>
#include<vector>
#include<map>
#include<algorithm>
#include<cmath>
#include<queue>using namespace std;const int maxn = 21;int n;struct Node{string str;int lchild,rchild;
}node[maxn];int vis[maxn] = {0};int R;int findRoot(){int root;for(int i=1;i<=n;i++){if(vis[i]==0){root = i;break;}}return root;
}void DFS(int root){if(root==NULL)return;if(root!=R&&(node[root].lchild!=-1||node[root].rchild!=-1))printf("(");if(node[root].lchild!=-1)DFS(node[root].lchild);printf("%s",node[root].str.c_str());if(node[root].rchild!=-1)DFS(node[root].rchild);if(root!=R&&(node[root].lchild!=-1||node[root].rchild!=-1))printf(")");return;
}int main(){cin>>n;for(int i=1;i<=n;i++){string str;int l,r;cin>>str>>l>>r;node[i].str = str;node[i].lchild = l;vis[l] = 1;node[i].rchild = r;vis[r] = 1;}R = findRoot();DFS(R);return 0;
}


http://lihuaxi.xjx100.cn/news/240846.html

相关文章

如何实现对象交互

在本篇随笔中&#xff0c;我们学习下什么是对象选择&#xff0c;投影和反投影是如何工作的&#xff0c;怎样使用Three.js构建可使用鼠标和对象交互的应用。例如当鼠标移到对象&#xff0c;对象变成红色&#xff0c;鼠标移走&#xff0c;对象又恢复原来的颜色。 本篇随笔的源代码…

微信公众平台开发(十一) 功能整合

一、简介 在前面的几篇微信功能开发文档中&#xff0c;各个微信的功能都是独立的&#xff0c;单一微信只能提供一种功能&#xff0c;这样不符合大众开发者和客户的需求。所以在这一篇文章中&#xff0c;我们将对前面开发出来的微信功能进行简单整合&#xff0c;以供读者参考。 …

漫谈回溯(未完待续)

将不使用优化算法、直接用朴素算法来解决问题的做法称为暴力法。 回溯是带优化的穷举。 回溯是具有界限函数的深度优先搜索。

iOS开发 最近开发了蓝牙模块,在此记录总结一下

为什么80%的码农都做不了架构师&#xff1f;>>> 1.基本概念 <1>中心者模式&#xff1a;常用的&#xff08;其实99.99%&#xff09;就是使用中心者模式作为开发&#xff0c;就是我们手机作为主机&#xff0c;连接蓝牙外设。由于开发只用到了中心者模式&#x…

Unity接入安卓sdk查看应用内存占用

注&#xff1a;若不清楚如何在unity中接入android sdk可先了解下相关流程。项目地址&#xff1a;http://download.csdn.net/download/yhuangher/9976564 在项目后期进行内存优化&#xff0c;在android端进行内存优化时做了若干辅助工具&#xff0c;比如此款&#xff0c;查看系统…

1115 Counting Nodes in a BST

我的DFS void DFS(Node* root){if(rootNULL)return;if(root->lchild){root->lchild->layer root->layer1;cnt[root->lchild->layer] ;maxLayer max(maxLayer,root->lchild->layer);DFS(root->lchild);}if(root->rchild){root->rchild->…

CSS控制字体在一行内显示不换行

当一行文字超过DIV或者Table的宽度的时候&#xff0c;浏览器中默认是让它换行显示的&#xff0c;如果不想让他换行要怎么办呢&#xff1f;用CSS让文字在一行内显示不换行的方法&#xff1a; 一般的文字截断(适用于内联与块)&#xff1a; 1 .text-overflow { 2 display:bloc…

Linux下des对称性加密

最近对接公安审计一些经历 对方的需求&#xff1a; 打成zip包对zip包进行des-cbc对称性加密&#xff0c;使用约定好的 -K和-iv值比如 -K "abcd$#!" -iv "efgh$#!"加密后做base64编码起初是想尝试用 php 去做&#xff0c;经过一阵折腾之后发现&#xff0c;p…