紫书例题6-7 树的层次遍历

纯小白也能看懂的代码,一起努力

6.3.2 二叉树的层次遍历

例题6-7 树的层次遍历(Trees on the level, Duke 1993, UVa 122)

输入一棵二叉树,你的任务是按从上到下、从左到右的顺序输出各个结点的值。每个结点都按照从根结点到它的移动序列给出(L表示左,R表示右)。在输入中,每个结点的左括号和右括号之间没有空格,相邻结点之间用一个空格隔开。每棵树的输入用一对空括号“()”结束(这对括号本身不代表一个结点),如图6-3所示

注意,如果从根到某个叶结点的路径上有的结点没有在输入中给出,或者给出超过一次,应当输出-1。结点个数不超过256。

样例输入:

(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()

(3,L) (4,R) ()

样例输出:

5 4 8 11 13 4 7 2 1

-1

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<vector>
  4 #include<queue>
  5 using namespace std;
  6 const int maxn = 256;
  7 char s[maxn];       //保存读入节点
  8 bool failed;
  9
 10 struct Node
 11 {
 12     bool have_value;
 13     int v;          //节点值
 14     Node *left, *right;
 15     Node():have_value(false), left(NULL),right(NULL){}
 16 };
 17 Node* root;         //二叉树根节点
 18 Node* newnode()     //创建新节点
 19 {
 20     return new Node;
 21 }
 22
 23 void addnode(int v, char* s)    //v为节点值,字符串s为LR加一个右括号)
 24 {
 25     int n = strlen(s);      //求传到函数里字符串的长度
 26     Node* u = root;         //根节点地址赋值给u
 27     for(int i = 0; i <n; i++)
 28         if(s[i] == ‘L‘)
 29         {
 30             if(u ->left == NULL)
 31             u ->left = newnode();
 32             u = u ->left;
 33         }
 34         else if(s[i] == ‘R‘)
 35         {
 36             if(u ->right == NULL)
 37             u ->right = newnode();
 38             u = u ->right;
 39         }
 40         if(u ->have_value)
 41             failed = true;
 42         u ->v = v;
 43         u ->have_value = true;
 44 }
 45
 46 bool read_input()
 47 {
 48     failed = false;
 49     root = newnode();
 50     for(;;)
 51     {
 52         if(scanf("%s", s) != 1)
 53             return false;
 54         if(!strcmp(s, "()"))
 55             break;
 56         int v;
 57         sscanf(&s[1], "%d", &v);        //从字符串中读入%d格式的内容存入v
 58         addnode(v, strchr(s, ‘,‘)+1);   //strchr(s, ‘,‘)是找到逗号的地址,+1是LR
 59     }
 60     return true;
 61 }
 62
 63 bool bfs(vector<int>& ans)
 64 {
 65     queue<Node*> q;
 66     ans.clear();
 67     q.push(root);
 68     while(!q.empty())
 69     {
 70         Node* u = q.front();
 71         q.pop();
 72         if(!u->have_value)
 73             return false;
 74         ans.push_back(u ->v);
 75         if(u ->left != NULL)
 76         q.push(u ->left);
 77         if(u ->right != NULL)
 78             q.push(u ->right);
 79     }
 80     return true;
 81 }
 82
 83 void remove_true(Node* u)       //防止内存泄漏
 84 {
 85     if(u == NULL)
 86         return;
 87     remove_true(u ->left);
 88     remove_true(u ->right);
 89     delete u;
 90 }
 91
 92 int main()
 93 {
 94     read_input();
 95     vector<int>ans;
 96     bfs(ans);
 97     if(ans.empty())
 98         printf("-1\n");
 99     else
100     {
101         for(int i = 0; i < ans.size(); i++)
102             printf("%d\n", ans[i]);
103     }
104     printf("\n");
105     remove_true(root);
106     return 0;
107 }

原文地址:https://www.cnblogs.com/riven7/p/10354454.html

时间: 2024-08-14 04:34:53

紫书例题6-7 树的层次遍历的相关文章

数据结构:树的BFS,树的层次遍历! 按先序遍历创建一棵树,然后以层次遍历输出。

按先序遍历创建一棵树,以层次遍历输出 样例输入 A B # D # # C E # # F # # 样例输出 LevelOrder: A B C D E F 代码: #include <iostream> #include <queue> using namespace std; struct node { //表示一个树上的节点 char ch; node *left, *right; }; node* creat() { //以递归的方式构造一棵二叉树 node *root =

紫书例题6-3 (UVa 442)

题目地址:https://vjudge.net/problem/UVA-442 题目大意:汗颜,其实我是直接看紫书的中文题意的,大意就是计算两个矩阵乘法次数,设计线性代数知识,可自己百度矩阵乘法. 思路:栈+模拟,左括号开始入栈,右括号开始计算栈顶两个矩阵的乘法次数然后再将新矩阵的n,m入栈即可. AC代码: #include <iostream> #include <string> #include <stack> #include <cstring> u

紫书例题6-4 (UVa 11988)

题目链接:https://vjudge.net/problem/UVA-11988 题目大意:输入一串字符,并按照要求输出,遇到'['字符就将光标移动到开头,遇到']'字符就将光标移动到末尾. 思路: 题目不难懂,很明显的一个模拟就行,重点是如何取存储,这里选择使用链表,链表的具体定义可以去百度看一下,这里不做过多解释,可以简单理解为一个未知长度的数组,它可以借助指针在任意位置插入(删除). 这题具体的操作可以用草稿纸模拟一遍,我是看一位博主的blog明白的,原blog地址:https://bl

树的层次遍历(Java代码实现)

与树的前中后序遍历的DFS思想不同,层次遍历用到的是BFS思想.一般DFS用递归去实现(也可以用栈实现),BFS需要用队列去实现. 层次遍历的步骤是: 1.对于不为空的结点,先把该结点加入到队列中 2.从队中拿出结点,如果该结点的左右结点不为空,就分别把左右结点加入到队列中 3.重复以上操作直到队列为空 1 public class Solution{ 2 class TreeNode { 3 int val; 4 TreeNode left; 5 TreeNode right; 6 TreeN

树的层次遍历(Trees on the level,UVA 122)

题目描述: 题目思路: 1.用结构链表来建树 2.用队列来实现层次遍历,当遍历到根节点时,将其子节点压入队列 #include <iostream> #include <cstdlib> #include <cstring> #include <vector> #include <queue> using namespace std; //树结点 struct Node{ int v ; Node* left,*right ; int have_

1127 ZigZagging on a Tree (30 分)树的层次遍历

1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to pr

树——通用树的层次遍历

1,为何及如何按层次遍历通用树中的每一个数据元素? 1,通用树结构是一种容器类型的树结构,其用来装数据元素,所以应该提供一种方法来遍历树中的每一个数据结构: 2,往下分析: 2,当前的事实: 1,树是非线性的数据结构,树的结点没有固定的编号方式: 1,也就不能像链表一样统一编号来访问: 3,新的需求: 1,为通用树结构提供新的方法,快速遍历每一个结点: 4,设计思路(游标): 1,在树中定义一个游标(GTreeNode<T>*): 2,遍历开始前将游标指向根结点(root()): 3,获取游标

Uva 122 树的层次遍历 Trees on the level lrj白书 p149

是否可以把树上结点的编号,然后把二叉树存储在数组中呢?很遗憾如果结点在一条链上,那将是2^256个结点 所以需要采用动态结构 首先要读取结点,建立二叉树addnode()+read_input()承担这样的工作 然后遍历二叉树,读取结点编号输出bfs() 这道题有内存池应用的背景 附链接  http://blog.csdn.net/shawngucas/article/details/6574863 #include <cstdio> #include <cstring> #inc

例题6-7 树的层次遍历

Trees on the level Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateof-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many