题意:给出一个语法树(二叉树),输出相应的中缀表达式。

思路:很显然,通过中序遍历来做。通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可。

代码:

#include <cstdio>
#include <cstring>
struct Node{
    ];
    int left,right;
}Tree[];
;//根结点

void inOrderTraversal(int v)
{
    ){
         || Tree[v].right!=-)) printf("(");
        inOrderTraversal(Tree[v].left);
        printf("%s",Tree[v].data);
        inOrderTraversal(Tree[v].right);
         || Tree[v].right!=-)) printf(")");
    }
}

int main()
{
    ];
    memset(isRoot,true,sizeof(isRoot));
    int n;
    scanf("%d",&n);
    ;i<=n;i++){
        scanf("%s %d %d",Tree[i].data,&Tree[i].left,&Tree[i].right);
        ) isRoot[Tree[i].left]=false;
        ) isRoot[Tree[i].right]=false;
    }
    while(isRoot[root]==false) root++;
    inOrderTraversal(root);
    ;
}

1130 Infix Expression的更多相关文章

  1. PAT甲级 1130. Infix Expression (25)

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  2. PAT 1130 Infix Expression[难][dfs]

    1130 Infix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspond ...

  3. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

  4. PAT 甲级 1130 Infix Expression

    https://pintia.cn/problem-sets/994805342720868352/problems/994805347921805312 Given a syntax tree (b ...

  5. PAT 1130 Infix Expression

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  6. 1130. Infix Expression (25)

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  7. PAT甲题题解-1130. Infix Expression (25)-中序遍历

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT1130:Infix Expression

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  9. A1130. Infix Expression

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

随机推荐

  1. Mac OS X下实现结束占用某特定端口的进程

    ---恢复内容开始--- 1.打开终端,使用如下命令: lsof -i:**** 以上命令中,****代表端口号,我们首先要知道哪个(或哪些)进程占用该端口,比如你可以运行 lsof -i:8000, ...

  2. python random使用生成随机字符串

    应用python random标准库做一个随机生成密码的程序,可以随机生成任意多个字符.(基于python2.7,如果是python3需要修改下) 案例: #-*-coding:utf-8 -*-#a ...

  3. Ajax-05 使用XMLHttpRequest和jQuery实现Ajax实例

    需求: (django)使用XMLHttpRequest和jQuery实现Ajax加法运算 url.py: from django.conf.urls import url from hello im ...

  4. memcache笔记

    服务端: 通过printf配合nc向memcached中写入数据[root@yz6245 ~]# printf "set key1 0 0 6\r\noldboy\r\n" |nc ...

  5. iptables(一)iptables概念

    为什么想写这个系列呢?openstack中的安全组.防火墙都是用iptables实现,后面的规则我们如果想要完全理解,不懂iptables的话,或者不精通iptables的话,会看的比较吃力.所以下定 ...

  6. 关于inline函数

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4975474.html 今天我在优化公司项目代码的过程中,借助了Intel的VTune ...

  7. 如何将桌面的路径定义到其它盘符,如d:\users\桌面

    首先要在“开始”——“运行”内输入“regedit”打开注册表编辑器,然后要在“文件”下拉菜单中的“导出”功能备份好注册表,以防万一,接着在左侧窗口依次打开: HKEY_CURRENT_USER\So ...

  8. Handling CLIK AS3 Compile Error 1152 & 5000

    Handling CLIK AS3 Compile Error 1152 & 5000 Action You add a CLIK AS3 component from CLIK_Compon ...

  9. Shell 参数(1)

    shell 中参数相关: ./a.sh a b c d $# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ ...

  10. struts1和struts2原理解析

    1.struts1和struts2 是2个完全不同的框架 其实struts2核心就是 webwork框架 struts1以ActionServlet作为核心控制器,由ActionServlet负责拦截 ...