题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include <cstdio> #include <cstring> #include <cstdlib> const int maxn = 10001; char str[maxn]; int p; struct Node { char data; Node* l; Node* r; };…
画图出来后结果很明显 xyPzwIM abcABdefgCDEF sample output wzyxIPM gfCecbDdAaEBF * + - x y z w F B E a A d D b c e C f g #include<cstdio> #include<cstring> #include<iostream> #include<string> #include<algorithm> #include<stack> #in…
https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each inte…
题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 200 Problem Description Trees are fundame…
题目描述: 题目思路: 1.用结构链表来建树 2.用队列来实现层次遍历,当遍历到根节点时,将其子节点压入队列 #include <iostream> #include <cstdlib> #include <cstring> #include <vector> #include <queue> using namespace std; //树结点 struct Node{ int v ; Node* left,*right ; int have_…
题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出(L表示左,R表示右) 在输入中,每个结点的左括号 和右括号之间没有空格,相邻结点之间用一个空格隔开.每棵树的输入用一对空括号)()结束  入上图所示: 注意:如果从根结点到某个叶节点的路径有的结点没有在输入中给出,或者给出超过一次,输出not complete.  结点个数不超过256 思路:显然…
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了内存,所以在bfs的时候如果该结点有内存,但是未赋值,那就算not complete,别忘了释放内存,虽然这不影响AC,但是注意内存泄漏是好习惯. PS: strchr函数原型:char * strchr(char * str, int ch); 功能就是找出在字符串str中第一次出项字符ch的位置…
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优先遍历保存权值最后输出. */ #include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; ; bool failed; struct NODE…
题意: 给定一颗树, 按层次遍历输出. 分析: 用数组模拟二叉树, bfs即可实现层次遍历 #include <bits/stdc++.h> using namespace std; struct Node{ bool have_value; int v, left, right; void reset(){//用于再次初始化节点 init(); } void init(){ have_value = ; v = left = right = ; } Node():have_value(),l…
7-4 Cartesian Tree (30分)   A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 1…