UVA - 699 The Falling Leaves 二叉树
来源:互联网 发布:linux中cat命令语法 编辑:程序博客网 时间:2024/04/28 03:36
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19244
题意:给定一棵二叉树,把根节点标号成0,然后每往左走标号就减1,每往右走标号就加1,问相同标号的节点的值得和,按标号的大写依次输出
思路:输入挺坑的,不过看了一会,可以边输入边建树,碰到其他值要接着往下递归建树,碰到-1就不用递归了。判断是不是结束,只要判断根节点是否为空就可以了。建树的时候顺便给每个点标号,然后用map<int, int> 去映射标号与节点值和,最后依次输出就好
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <map>#include <vector>#include <set>using namespace std;const int N = 1100;const int INF = 0x3f3f3f3f;int cas = 0;int a;map<int, int> mpa;struct node{ int v, sign; node *lc, *rc; node(int v = 0, int sign = 0, node *lc = NULL, node *rc = NULL): v(v), sign(sign), lc(lc), rc(rc){}};void build(node* &root, int cnt){ scanf("%d", &a); if(a == -1) return; //不用建当前节点,也不用往下递归了 root = new node(a, cnt); build(root ->lc, cnt - 1); build(root ->rc, cnt + 1);}void dfs(node *root){ if(root != NULL) { mpa[root->sign] += root ->v; dfs(root ->lc); dfs(root ->rc); }}int main(){ while(true) { node *root = NULL; mpa.clear(); build(root, 0); if(root == NULL) break; //根节点为空,结束 else { dfs(root); printf("Case %d:\n", ++cas); bool flag = false; for(map<int, int> :: iterator p = mpa.begin(); p != mpa.end(); p++) //依次输出 { if(! flag) printf("%d", p ->second), flag = true; else printf(" %d", p ->second); } printf("\n"); } printf("\n"); } return 0;}
0 0
- Uva 699 - The Falling Leaves//二叉树
- UVA - 699 The Falling Leaves 二叉树
- uva 699 The Falling Leaves 二叉树
- UVa 699 - The Falling Leaves 二叉树的落叶
- UVa 699 The Falling Leaves (DFS遍历二叉树)
- uva 699 The Falling Leaves (二叉树)
- Uva-699 - The Falling Leaves(二叉树与递归)
- UVA 699The Falling Leaves(二叉树)
- UVA 699 The Falling Leaves(二叉树|DFS)
- uva 699 - The Falling Leaves
- uva 699 The Falling Leaves
- uva 699 - The Falling Leaves
- UVa 699 - The Falling Leaves
- UVA 699 - The Falling Leaves
- UVa 699 - The Falling Leaves
- uva 699 The Falling Leaves
- uva-699 The Falling Leaves
- UVA 699 The Falling Leaves
- Linux下常用命令大全
- 欢迎使用CSDN-markdown编辑器
- 注册广播接收者两种方式的区别,及优缺点
- 个人网站搭建—环境配置(ISS、PHP、MySQL)
- Linux内核中读写文件
- UVA - 699 The Falling Leaves 二叉树
- 数据类型以及变量与常量
- Tkinter教程之Message篇
- suse linux怎么修改主机名
- UML的9种图例解析
- servlet 多线程问题
- redis学习笔记 发布与订阅
- Ubuntu Install Pygame
- 8、数据的操作:插入、更新、删除----第2篇